At some point you may need to stash your changes in a dirty working directory. For example when you did not commit your
local changes yet but you want to pull in changes from a remote repository. Or you quickly want to checkout a different
branch without the need of committing your progress.
For those situations, stashing is the recommended way to do so. In this post I will give you a brief overview of what
stashing in git is and I will demonstrate the basic workflow. To fully understand the given examples, a fundamental
knowledge about git is required.
Stashing enables you to save your local changes away and resets your working directory to the HEAD state. Stashed
changes can be re-applied whenever you need them. It is even possible to use stashes multiple times.
Create
Given you made changes (files have to be tracked, but it does not matter if staged for commit or not) in a git directory
which contains at least one commit:
To create a stash use git stash (or the equivalent git stash push). This will store your changes away and your
working directory is clean again:
It is also possible to add a description to a stash, which makes identification easier. You have to use the longer
version git stash push and add -m <message>:
List
git stash list displays a list of all your stashes:
Show
Inspect the latest stash using git stash show:
If you have multiple stashes, provide an index to inspect a specific one:
Pop
By using git stash pop the latest stash will be removed from the stash list and applied to your working directory
(provide an index to pop a specific stash):
Apply
git stash apply adds the latest stash to your working directory but keeps the stash remaining in the list for further
usage (provide an index to apply a specific stash):
Drop
To remove the latest stash from the list without applying it, use git stash drop (provide an index to drop a specific
stash):
Oh My Zsh aliases
A workflow tip from my side to close up this post - use Oh My Zsh! It comes with a bunch of useful and convenient
aliases for git, including shortcuts for stashing: