DEV Community

Srebalaji Thirumalai
Srebalaji Thirumalai

Posted on • Originally published at gitbetter.substack.com on

How to use git stash effectively

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

List all git stashes

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

git stash list
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode

To view the diff changes of stash, you can use

git stash show -p 
git stash show stash@{10} -p
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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}
Enter fullscreen mode Exit fullscreen mode
Using pop

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

git stash pop
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

This will delete the latest stash.

git stash drop stash@{10}
Enter fullscreen mode Exit fullscreen mode

This will drop the particular stash.

Create a branch from git stash

git stash branch <branch_name> stash@{5}
Enter fullscreen mode Exit fullscreen mode

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

Thank you for reading :)

If you got any new tricks you share, you can share it in the comments :) :)

This post was originally posted in the newsletter GitBetter. If you are interested in leveling up your game in Git, you can subscribe to it.

Top comments (0)