Useful tricks of git stash

How to use git stash effectively

If you are using Git for a while, you might have come across git stash.

Git stash helps you to temporarily save the changes in the working directory so that you will get a clean directory. Then you can apply those changes later in your git workflow.

Lets’s see an example

You can see that there are some changes in the working directory. You can use git stash to save all the changes so that the working directory will be clean.

git stash

After stashing, you can see that the changes in the tracked files are gone in the working directory. And also note that the untracked files are still present.

Those saved changes can be accessed and used in the future or else kept aside forever.

By default, stash won’t save the untracked files. But if you still need to stash the untracked files you can use

git stash -u

List all git stashes

You can use git stash multiple times across the git directory. To view all your git stashes

git stash list

In the above image, you can see the list of git stashes.

Each stash will have a name, the working directory, and the HEAD commit.

Git stash name and the branch are very important. This helps you to identify the stash easily.

Remember that stash works like stacks. The most recent stash will be at the top.

View stash changes using show

It will be useful to view the changes which have been stashed.

git stash show

The above command will show the changes in the latest stash (i.e) stash@{0}

To view the changes of any particular stash

git stash show stash@{2}

To view the diff changes of stash, you can use

git stash show -p 
git stash show stash@{10} -p

Applying stashed changes in the working directory

Now you know how to stash the changes let’s see how to apply those changes in the working directory.

git stash apply

The above command will take the latest stash (i.e) stash@{0} an apply it to the working directory.

In the above image, you can see that changes in the latest stash are applied.

To apply the particular stash, you can use

git stash apply stash@{5}

Using pop

There is also another option to applying the stash in the working directory.

git stash pop

This command will apply the changes of the latest stash and will also delete the stash from the stash list.

Use this command when you don’t need the stash anymore after applying the stash.

Use apply when you need to apply the stash and still, you don’t want to delete the stash from the list.

Delete a stash

git stash drop

This will delete the latest stash.

git stash drop stash@{10}

This will drop the particular stash.

Create a branch from git stash

git stash branch <branch_name> stash@{5}

This command will create a new branch with the specified stash name. And this will delete the stash from the list like pop.

That’s it for today :)

Before signing up, I’d like to know something. Will you be interested in learning more about how people start successful open source projects, how they grow, how they collaborate, and what it is to be a contributor to open-source projects?

Reply to this email.

See you next week :) :)