git stash
09 Dec 2024What does git stash do and how to use it?
What is git stash?
Do you have a work in progress but don’t want to commit yet? If the files you modified blocking you to change branches, git stash is there to rescue. It helps you to stash those changes, so you can switch branches and come back to where you left off and apply later on.
Scope of the article
There are some practices I follow that helps keeping the track and housekeeping easier. Rest of the git stash features won’t be in the scope of this article.
Practices I follow / Prerequisites
- I stash in relevant branch. Not in
development, not inmaster. (Meaninggit checkout your-branch-namebefore following the article.) - I stash only tracked files. (Meaning
git add your.filebefore following the article.) - I stash with a message.
- I remove the stash when I am done with it.
How to stash staged changes with a message?
git stash --staged -m "Work in progress for X feature."
List the stashes
git stash list
It will show you a list of all stashed changes, like:
stash@{0}: On your-branch-name: Work in progress for X feature.
Apply and remove the stash
In order to bring back what is stashed in stash@{0} simply run below command, using only stash index number.
git stash pop 0
Rinse and repeat!
More Info
Just these three commands are enough for me to get what I want from git stash, but you can do more. Here you can find the official documentation:
https://git-scm.com/docs/git-stash