Git Stash - Saving Your Changes

Umer Mansoor Umer Mansoor Follow Apr 18, 2016 · 4 mins read

Let’s say you are in the middle of implementing a new feature. You’re half way through your changes and the code is in a messy state. You get a message that there’s an urgent issue that requires you to switch gears and work on it immediately. You don’t want to commit your half baked changes but also don’t want to lose your work because you want to revisit it at a later time? What do you do?

The answer to this problem is the git stash command.

Running git stash will take the changes you’ve made to tracked files in the working directory as well as staged changes and saves them to a stack. You can reapply these changes from the stack at any time. After stashing, you’ll end up with a clean working directory and can freely switch branches and work on something else. Let’s walk through a complete example.

Let’s say we’re in the middle of editing file.txt when we get a Slack message to switch to something else right away. We’ll use git stash to save our changes.

$ echo "Improvement 1 of 3" >> file.txt
$ git status
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
          modified:   file.txt
$ git stash save "Partial improvement to file.txt"   
$ git status
# On branch master
nothing to commit, working directory clean              

After stashing, you’re free to do whatever you like. You can switch to a different branch, make your changes, commit and push them to the remote repo. After you’re done, you’re ready to resume working on file.txt which you stashed away. To reapply, you will use the git stash apply command.

$ git stash apply             

Assuming you don’t run into merge issues, you should get your partial changes back. If not, git will throw a merge conflict error if it can’t reapply your changes safely. We’ll look at how to resolve merge conflicts in the next chapter.

You can stash multiple times. Each time you run git stash it will save a new stash on the stack. To see a list of all the stashes stored on the stack, use the git stash list command.

$ git stash list
stash@{0}: WIP on master: d724198 partial improvement 2
stash@{1}: WIP on master: d724198 bug fix for Unity
stash@{2}: WIP on master: c9a03f4 added partial improvement 1

The stashes are ordered by most recent to newest (hence it is a stack.) stash@{0} is the most recent and stash@{2} is the oldest in the example above. To restore the very first stash you saved:

$ git stash apply stash@{2}

A stash could be applied to any branch not just the same branch it was saved from. Also note that stash will ignore ‘un-tracked’ files. If you added a new file, you must first add it to the index using git add before stashing.

Merge conflicts

There are times when git stash apply won’t work and throw a merge conflict. E.g.

$ git stash apply
error: The following untracked working tree files would be overwritten by merge:
Please move or remove them before you merge.

The easiest way to get out of merge conflicts is to apply your stash to a new branch. To do this you can use git stash branch <new branchname>. This will create a new branch, check out the commit when you stashed your changes, reapply your stash on top of it.

$ git stash branch temp_restore

That’s pretty much it. Here are some bonus tips for git stash:

  • To save your stash with a message or give it a name you can use the following syntax: git stash save <message>. For example: git stash save "feature orca-654".
  • By default, stash doesn’t save untracked files. You could either stage them or save with the -u switch e.g. git stash save -u
  • To delete the stash after it has been applied, you can use the git stash pop command e.g. git stash pop stash@{2} will apply stash@{2} and delete it from the stack.
  • To delete a stash without applying it, use git stash drop e.g. git stash drop stash@{0}
  • Use git stash show to see a summary of diffs.
  • To delete all your stashes, use the git stash clear command. Be careful because this is dangerous command. You will lose your stashes forever.

That’s all. If you enjoyed this article, please share it with your friends. The links to share on Facebook, Twitter, LinkedIn are below.


You May Also Enjoy

If you like this post, please share using the buttons above. It will help CodeAhoy grow and add new content. Thank you!

Speak Your Mind