Git Stash Tutorial
What is Git Stash?
Git stash allows you to save a draft of your in-progress changes and revert your working directory back to a clean slate. In other words, stash will take any un-commited changes on your working directory, put them aside, and revert your working directory back to match your HEAD commit.
Stash is typically used when you want to temporarily put files aside to do a remote pull, or to merge/rebase a branch. It’s also useful as a workflow tool allowing you to save a draft of your work. Git stores stashes in a stack-like structure, and multiple stashes can be stored.
Git Stash Cheatsheet
Basic Git Stash Workflow
Ensure all your files are tracked by Git (if you’ve created new files since your last commit, telling Git about them will include them in a stash)
git add .
Use stash to set them aside
When you’re ready to apply your stashed changes back into your working directory:
git stash pop
pop will remove your stash from the stack after applying the changes; however, using
git stash apply instead will still apply your changes, but persist the stash entry as well.
Show your stashes
To see a list of your stashes:
git stash list
To examine what files are contained within your stashes (where
<index-num> is the index number of the stash you’d like to examine):
git stash show <index-num>
Add a stash message
By default, Git will give your stash a generic name. To rename a stash, use the
-m flag while stashing:
git stash -m '<stash name>'
Applying specific stashes
If you’d like to apply a stash that’s not on top of your stash list, you can pass it’s index number to
git stash pop --index <index-num>
Or simply drop the
git stash pop <index-num>
Create a branch from a stash entry
Create a new branch with changes from the stash anchored at the commit where the stash was originally created:
git stash branch <branch-name> <stash-index-num>
Fix Git stash merge conflicts
If changes introduced prior to applying your stash conflict with files from that stash, you’ll have a merge conflict.
To learn how to fix a stash merge conflicts, check out the dedicated article I have on the subject.