How to Stash Git Changes
Don’t know how to stash the dirty state of the working directory and save it on a stack of incomplete changes? See how to stash with the given steps.
There are times when you need to switch branches to work on another project. The git stash command comes to the rescue. It is used to stash the dirty state of the working directory and saves it on a stack of incomplete changes that can be re-applied later.
Steps to stashing changes
Let’s see what commands are required for stashing your changes:
Displaying the state
First, run git status to see the current state:
git status to see dirty state
git statusStashing Your Work
Run <kbd class="highlighted">git stash</kbd> to stash the changes:
git stash the changes
git stashThe <kbd class="highlighted">git stash</kbd> command can also be used to create multiple stashes. Use git stash list to view them. The code looks like this:
git stash create multiple stashes
git stash listRe-applying Your Changed Stashes
Run git stash pop to remove the changes from your stash and re-apply them to your working copy.
remove changes from your stash and re-apply to working copy
git stash popRun git stash apply if you want to re-apply the changes and keep them in your stash:
reapply the changes and keep them in the stash
git stash applyStashing Changed Stages
If you want to restore the index state along with the working directory changes, use the <kbd class="highlighted">git stash apply</kbd> command with the --index option:
git stash apply with --index option
git stash apply --indexCleaning up Stashes
Run git stash drop with the name of the stash to remove it:
stash drop to remove a specific stash
git stash drop <stash_name>Un-applying a Stash
If you want to un-apply the most recent stash, use the following:
cancel the most recent stash
git stash show -p | git apply -RStash Meaning
The <kbd class="highlighted">git stash</kbd> command shelves changes you have made to your working copy so you can work on something else, and then come back to re-apply them. It takes both staged and unstaged uncommitted changes, saves them away for further use, and then removes them from your working copy.
Multiple Stashes
As mentioned above, you can also create multiple stashes. By default, stashes are identified as "WIP" – work in progress. They are stored on top of the branch and commit from which you created the stash.
Staged and Unstaged Changes
Running <kbd class="highlighted">git stash</kbd> will stash both changes not staged for commit and the ones staged for it. However, git stash will not stash new files that have not yet been staged, nor files that have been ignored. In those cases, the -u option (or --include-untracked) helps to also stash untracked files.
also git stash untracked files
git stash -uYou can include changes to ignored files as well by using the -a option (or --all) when running git stash.
include ignored files in stash
git stash -aHow to Apply Patch
Git does not support a stash unapply command, but it is possible to unapply a stash by retrieving the patch associated with it and applying it in reverse:
reverse patch to unapply a stash
git stash show -p stash@{0} | git apply -RGit assumes the most recent stash if you don’t specify a stash:
git most recent stash
git stash show -p | git apply -R