Git Stash — Basics of Git
While working on a feature in a project, Do you ever felt any one of the following scenarios?
- Oh! I might have made these changes before
- I should have commit that part first and start doing this feature
- I must have merged that branch before touching this feature
- I should have made a commit in that particular branch and push the changes and continue working on this feature
If yes, then git stash
command is there for you to help.
What is Git Stash?
git stash
temporarily stores the changes (shelves / stashes) you’ve made on your working copy. This gives you the freedom to work on something else and re-apply your changes when you’re back.
Fascinating!!!! Isn’t it?
Let’s understand more by digging deeper
git stash
is handy, when you have to switch context and work on some other part of your project, but you’ve already started building a new feature or fixing a bug or cleaning a tech debt and you’re not in a logical state to commit your changes.
Stash your work
Simple git stash
command saves your staged and unstaged changes (not yet committed) for later use and removes them from your working copy. Simple git stash
command doesn’t touch the untracked and ignored files.
I’ve created a sample project and made some changes.
But, I want a commit to go before this. So, I’ll stash this change.
You can see that my changes are reverted after applying git stash
command.
Let’s make a commit now.
Now that we have made a commit. Let’s bring our changes back.
Stash Pop
git stash pop
command brings back your latest stashed changes from git. Let’s bring our changes back.
Q & A
Remembering your quote,
Simple
git stash
command doesn’t touch the untracked and ignored files.
What if I have to stash the untracked and ignored files?
This can be achieved with using option flags with the git command.
Adding -u
or --include-untracked
option with git stash
command will include the untracked files on stashing
and
Adding -a
or --all
option with git stash
command will include the ignored files on stashing
In the below screenshot, I have created two files in our repo.
File3.txt -> Untracked file
Hello.txt -> Ignored file
Let’s stash both the files using the above options
I ran the commands (-u and -a) individually, instead you can run both the commands together ( git stash -u -a
)
You made two stashes above. How can I see all my stashes together?
git stash list
command will list the history of stashes stored in the repo.
Note: This command will not show the popped stash
Can I see the changes made in each stash?
Yes. You can. git stash show <stash_id>
will show the changes made in that particular stash.
I made a change to File1.txt file.
Adding -p
option flag to show
command will show the line by line changes.
Is there a possibility to pop a particular stash? Say, the middle stash item.
Yes. Absolutely you can do.
Simply add the stash-id at the end of stash pop
command
After running the command, you can see the middle stash entry has been removed.
How to save the stash with a message? It would be helpful for me to identify quickly and pop that particular commit in the future.
Yes. You’re right. We have a way in Git to save the stash with a customized message.
git stash save <message>
command saves the stash entry with the customized message. You can enclose the message in double quotes (“”), if your message has spaces or special characters in it.
I need to pop the stash entry but it should not remove the entry from the stash list. Is it possible?
Yes. It is.
You can achieve that with git stash apply
command. This command will apply the changes from that stash to your working repo and will not remove the entry from the stash list.
You can see from the above screenshot that the last stash was not removed, and the changes made in that stash and pulled into the working repo.
Will I be able to remove a particular entry from stash?
Yes. Obviously, you can. If you’re able to create it, you can also delete the same.
git stash drop
command helps you to remove a entry from stash list. Just running git stash drop
will remove latest entry from stash list. You can add a stash-id
with the drop
command to remove the particular entry.
Tip
If you wish to remove all the stash entries, you can achieve it by simply running git stash clear
command.
Hope you enjoyed reading this article. If you have any questions or suggestions, please post them in the comments. Give a clap 👏, if you liked and learnt something new.
Subscribe to our newsletter to receive more such insightful articles that get delivered straight to your inbox.
Happy Coding