Using Git Stash to Cache Your Pending Changes
This is just a quick tip which i stumbled upon this weekend. Let’s say you are working on a git branch with a unfinished amount of changes. Now you want to switch branches i.e. to lookup some older/newer value, but you do not want to commit your unfinished piece of work. The solution? git stash
.
git stash
Let’s say your Project changes look like this atm:
The Usage is pretty simple just type use git stash:
And your working directory is clean (last commit):
Be aware that git stash also saved the unstaged changes so far. Now you can switch branches and do your stuff…
If you want to reapply your stash just type: git stash apply
By default that would ignore the staging you’ve done so far, so to also restage your changeset use git stash apply --index
If you have multiple stashes git stash apply
will always pick the latest.
You can also have multiple stashes and unapply some subset of them. But that goes beyond that blog post. If you want to learn more just read the wonderful ProGit Book by Scott Chacon.