Git : Stashing

Suppose you are working on a new feature of your product in your feature branch and suddenly you get a call from your boss to fix a critical bug in production or some other escalation for which you have to switch to a different branch. You cannot commit your partial code and also cannot throw away your changes. So you need some temporary space, where you can store your partial changes and later on commit it.  This is where stashing comes in. Stashing allows you to take your files (committed and uncommitted files) and stores them in an temporary area so as you can restore them later on. 

So in our scenario we have 3 branches- master,features, hotFixes and we are currently working on features branch.


Now we have to switch to hotfixes branch but we cannot commit incomplete changes. SO we do a
git stash save
This will move our changes to a temporary . You can simply do git stash also, its same as git stash save


As you can see after stash our changes are gone and git status shows nothing. We can then switch to our hotfixes branch and do our work. After we completed this we go back to our features branch and run
git stash apply


Whenever you do a git stash, git pushes that stash on to a stack. You can check the stack by
$ git stash list


As you can see we have 2 in stack. It shows the branch of which the stash was made with the last commit that was made on that branch. The top of the stack will be applied by default if we don’t specify the name. If you want to apply a stash down the stack you can do
$ git stash apply stash@{1}

When we apply the stash , we should also drop it from the stash stack, so as keep our stack clean. You can do this by
$ git stash drop

Git gives us another command to do above 2 things in one.
$ git stash pop

Above command will do a git stash apply and then git stash drop.

Sometimes you apply stash after  you made some changes and then try to do a git stash apply. git will abort and ask you to commit your local changes first.


In another scenario you pulled or made some changes and committed them and then try to do a git stash apply. In this case git will merge automatically. If there is a merge conflict it will tell you the same which you have to do manually.


Note that if you do a git stash pop and a merge conflict arises, the stash wont be popped from the stack since it failed in apply phase itself. In that case you have to take care of that using drop.

Suppose you have changes made both in staging and unstage area. Now if you do a git stash, and later on do a git pop, it will move all your uncommitted changes to unstage area.


If you want to stash only your unstaged files, you can use
git stash save –keep-index


What if you have new files that are currently untracked. git stash wont stash it, it only going to stash files that git knows about. To include untracked files in our stash you can do

$ git stash save –include-untracked

Sometimes if your stash stack is big, it might be confusing which stash you want. You can do save your stash with a message so as to remember it easily.
$ git stash save “<your message>”

You can also use git stash show with many options (like git log) to know other details
$ git stash show


If you want to create a new branch from the stash, you can use
git stash branch <new-branch-name> stash@{0}


This will create a new branch out of the stash and automatically pop that stash out of the stack.

%d bloggers like this: