Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to stash in Git

📅 2016-Jun-14 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ git, stash ⬩ 📚 Archive

Stashing in Git is a very useful technique that eases daily work with code. Many a time, you are in the middle of writing or changing some code when you might be required to quickly checkout a different branch. For example, another team member wants you to quickly run a test in a different branch or fix a bug appearing in a different branch. It is pretty easy to clone a new repository to do these things if the repository is small and building files is quick. If your repository or build setup is large, complicated or time consuming, then stashing is a great addition to your workflow.

Stashing just saves all your staged or unstaged files that are changed and saves them up in your local repository. The stashed commits are actually stored locally in a reflog for a branch named stash. So, the git stash commands are just simple wrappers over standard git commands.

$ git stash

By default this stash commit is saved with a generic commit message that includes the branch, the commit hash and the log of the commit upon which the stash was created.

$ git stash save "WIP on enabling write to disk"
$ git stash list
$ git reflog stash
$ git stash pop
$ git stash apply
$ git stash apply stash@{0}
$ git stash apply stash@{99}
$ git checkout stash@{13} -- foobar/joe.cpp
$ git stash clear

That is all there is to it! This is one of the easier and straightforward commands in Git 😊

Tried with: Git 2.17.1


© 2022 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 @codeyarns@hachyderm.io📧