DEV Community

Srebalaji Thirumalai
Srebalaji Thirumalai

Posted on • Edited on

Useful tricks you might not know about Git stash

Keep calm and git stash

I have launched a newsletter Git Better to help learn new tricks and advanced topics of Git. If you are interested in getting your game better in Git, you should definitely check that out.

If you have been using Git for a while you might have used Git stash. It’s one of the useful features in Git.

Here are some of the useful tricks I learned about Git stash last week.

  1. Git stash save
  2. Git stash list
  3. Git stash apply
  4. Git stash pop
  5. Git stash show
  6. Git stash branch
  7. Git stash clear
  8. Git stash drop

Git stash save

This command is like Git stash. But this command comes with various options. I will discuss some important options in this post.

Git stash with message

          git stash save “Your stash message”.
Enter fullscreen mode Exit fullscreen mode

The above command stashes with a message. We will see how this is helpful in a bit.

Stashing untracked files

You can also stash untracked files.

          git stash save -u
Enter fullscreen mode Exit fullscreen mode

or

         git stash save --include-untracked
Enter fullscreen mode Exit fullscreen mode

Git stash list

Before discussing this command, let me tell you something about how stash works.

When you Git stash or Git stash save, Git will actually create a Git commit object with some name and then save it in your repo.

So it means that you can view the list of stashes you made at any time.

         git stash list
Enter fullscreen mode Exit fullscreen mode

See the example below:

git stash list example

You can see the list of stashes made. And the most recent stash made is in the top.

And you can see that the top stash is given a custom message (using Git stash save “message” ).

Git stash apply

This command takes the top most stash in the stack and applies it to the repo. In our case it is stash@{0}

If you want to apply some other stash you can specify the stash id.

Here’s the example:

         git stash apply stash@{1}
Enter fullscreen mode Exit fullscreen mode

Git stash pop

This command is very similar to stash apply but it deletes the stash from the stack after it is applied.

Here’s the example:

Git stash pop example

As you can see the top stash is deleted and stash@{0} is updated with older stash.

Likewise, if you want a particular stash to pop you can specify the stash id.

         git stash pop stash@{1}
Enter fullscreen mode Exit fullscreen mode

Git stash show

This command shows the summary of the stash diffs. The above command considers only the latest stash.

Here’s the example:
Git stash show example

If you want to see the full diff, you can use

         git stash show -p
Enter fullscreen mode Exit fullscreen mode

Likewise with other commands, you can also specify the stash id to get the diff summary.

         git stash show stash@{1}
Enter fullscreen mode Exit fullscreen mode

Git stash branch

This command creates a new branch with the latest stash, and then deletes the latest stash ( like stash pop).

If you need a particular stash you can specify the stash id.

         git stash branch <name> stash@{1}
Enter fullscreen mode Exit fullscreen mode

This will be useful when you run into conflicts after you’ve applied the stash to the latest version of your branch.

Git stash clear

This command deletes all the stashes made in the repo. It maybe impossible to revert.

Git stash drop

This command deletes the latest stash from the stack. But use it with caution, it maybe be difficult to revert.

You can also specify the stash id.

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

Hope you got some useful tricks about Git stash.

If you have come this far, I think you are pretty much interested in Git. Check out my newsletter Git Better to learn new tricks and advanced topics of Git. :)

If you enjoyed the article try to show some love and share it :)

Top comments (7)

Collapse
 
mahmoudhossam profile image
Mahmoud Hossam

git stash save has been deprecated in favor of git stash push

Read all about that here:

git-scm.com/docs/git-stash#git-sta...

Collapse
 
freeatnet profile image
Arseniy ✌️ Ivanov

Saving untracked files or specific paths is 🔑:

git stash save -u -- <path…>

Collapse
 
onecuteliz profile image
Elizabeth Jenerson

😒
You ain't lying. Made the mistake of git clean w/ unsaved untracked files cause I was a noob. 🙈

Collapse
 
freeatnet profile image
Arseniy ✌️ Ivanov

😟

I too learnt the hard way to use clean -n (dry-run of clean that just outputs what it would remove) before actually running it.

Collapse
 
peterchaula profile image
Peter Chaula

git stash pop stash@{N} is a bit strange to me. You are "popping" a stash which is buried below other stashes

Collapse
 
txai profile image
Txai

Really helpful article, thanks!!!

Collapse
 
devconnectedcom profile image
devconnected • Edited

Hello,

Thank you for the article!

I wrote also an article about Git stash, you might want to check it out!

Git Stash Changes