Master Git (part IV). Stash your changes
Imagine a situation when you start working on some part of your project, make a bunch of uncommitted changes, but something urgent comes up that requires you to quickly make a few commits concerning another part of the project. In such cases, instead of losing the work you have already done, you can use git stash command to save your uncommitted changes away for later use while switching to another task.
Stashing in git is simple. All you need to do is to run
$ git stash
and git will make it look like those uncommitted changes are gone and your repository is clean:
And as you see, to recover stashed changes you use command
$ git stash pop
The thing you should be aware of is that git doesn’t stash untracked files:
If we want to stash untracked file, we need to pass -u option:
You can stash more than once. In this case, you probably want to add messages to your stashes with
$ git stash save "message"
To see a list of all your stashes:
$ git stash list
You manage your stashes by first looking at their identifier with git stash list command and then using commands:
$ git pop <stash_id> # to re-apply a stash
$ git drop <stash_id> # to delete a stash
$ git stash clear # delete all stashes