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.
- Git stash save
- Git stash list
- Git stash apply
- Git stash pop
- Git stash show
- Git stash branch
- Git stash clear
- 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”.
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
or
git stash save --include-untracked
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
See the example below:
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}
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:
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}
Git stash show
This command shows the summary of the stash diffs. The above command considers only the latest stash.
If you want to see the full diff, you can use
git stash show -p
Likewise with other commands, you can also specify the stash id to get the diff summary.
git stash show stash@{1}
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}
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}
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)
git stash save
has been deprecated in favor ofgit stash push
Read all about that here:
git-scm.com/docs/git-stash#git-sta...
Saving untracked files or specific paths is 🔑:
git stash save -u -- <path…>
😒
You ain't lying. Made the mistake of git clean w/ unsaved untracked files cause I was a noob. 🙈
😟
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.git stash pop stash@{N} is a bit strange to me. You are "popping" a stash which is buried below other stashes
Really helpful article, thanks!!!
Hello,
Thank you for the article!
I wrote also an article about Git stash, you might want to check it out!
Git Stash Changes