You are in the middle of developing a feature and suddenly your manager tells you to work on an urgent fix for a production bug! You want to create a new branch for the fix but git wouldn't let you as you have uncommited changes. How can you switch to a new branch without losing your local uncommitted changes? Git Stash to your rescue.

Say, I’ve two commits in my git repository:
$ git log --oneline --decorate --graph
* 10c532b (HEAD -> master) Add File2 <- Commit #1
* d19fe8d Add File1 <- Commit #2
And I’ve those two files file1 and file2 in my directory.
$ ls
file1 file2
After I have added file2, I've made some changes to it that have not been committed yet, as you see from the output of git status below:
$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directo
modified: file2
no changes added to commit (use "git add" and/or "git commit -a")
At this stage, say I have to switch to a different branch or a different commit, I usually have two options. Either commit these changes or lose them by switching to the other commit. As I don’t want to pick either of those options, I will go with the third option available, which is Stashing.
Git stash, as the name indicates, lets you stash-away some changes temporarily. You can think of stashes as being "temporary commits".
You can stash your changes with the following command:
git stash save "Changes in file2"
To git stash, I pass in the command save along with a message. This message is similar to a commit messages, by which you can identify a particular stash.
You can do the same with just git stash as well. But with that, you will not be able to provide a stash message.
You can see the existing list of stashes with the list command:
$ git stash list
stash@{0}: On master: Changes in file2
That is the stash I’ve just created, and you can see the branch name master and the stash message I have given as well. The stash@{0} is the identifier for your stash.
Now that we have created the stash, to use it, we have two options.
apply— Adds the changes in stash to working directory but keeps the stashpop— Adds the changes in stash to working directory and deletes the stash
To apply a stash:
git stash apply stash@{0}
This will still keep the stash, and you will see it in the output of the list command.
To pop a stash:
git stash pop stash@{0}
If you want to delete the stash, you can do:
git stash drop stash@{0}
And that will delete the stash.
Git stashes are a great way to quickly stow away your unsaved changes for some later use. Try this out and this can be a really useful tool in your development workflow. One way in which I use stashes is to make a change on multiple branches. I stash the necessary changes and then apply the stash on all of the branches. Pretty neat!
That is all for this article.
For more programming articles, checkout Freblogg, Freblogg/Git
Thanks for reading. See you again in the next article.
Image attribution:
Git Logo - Git Logo by Jason Long is licensed under the Creative Commons Attribution 3.0 Unported License.