Git stash - temporarily shelves (or stashes) changes you've made to your working copy so you can work on something else, and then come back and re-apply them later on. Stashing is handy if you need to quickly switch context and work on something else, but you're mid-way through a code change and aren't quite ready to commit.
Stashing your work
The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy. At this point you're free to make changes, create new commits, switch branches, and perform any other Git operations; then come back and re-apply your stash when you're ready. Note that the stash is local to your Git repository; stashes are not transferred to the server when you push. Re-applying your stashed changes You can reapply previously stashed changes with git stash pop Popping your stash removes the changes from your stash and reapplies them to your working copy. Alternatively, you can reapply the changes to your working copy and keep them in your stash with git stash apply. This is useful if you want to apply the same stashed changes to multiple branches. Now that you know the basics of stashing, there is one caveat with
git stash 
you need to be aware of: by default Git won't stash changes made to untracked or ignored files. Stashing untracked or ignored files By default, running git stash will stash: changes that have been added to your index (staged changes)changes made to files that are currently tracked by Git (unstaged changes) But it will not stash: new files in your working copy that have not yet been staged, files that have been ignored. So if we add a third file to our example above, but don't stage it (i.e. we don't run
git add
),
git stash
won't stash it. Adding the -u option (or --include-untracked) tells git stash to also stash your untracked files. You can include changes to ignored files as well by passing the -a option (or --all) when running
git stash.
Managing multiple stashes You aren't limited to a single stash. You can run
git stash
several times to create multiple stashes, and then use git stash list to view them. By default, stashes are identified simply as a "WIP" – work in progress – on top of the branch and commit that you created the stash from. After a while it can be difficult to remember what each stash contains. To provide a bit more context, it's good practice to annotate your stashes with a description, using git stash save "message" By default, git stash pop will re-apply the most recently created stash: stash@{0} You can choose which stash to re-apply by passing its identifier as the last argument, for example: git stash pop stash@{2} Viewing stash diffs You can view a summary of a stash with git stash show Or pass the
-p
` option (or --patch)to view the full diff of a stash.
Creating a branch from your stash
If the changes on your branch diverge from the changes in your stash, you may run into conflicts when popping or applying your stash. Instead, you can use git stash branch to create a new branch to apply your stashed changes to.This checks out a new branch based on the commit that you created your stash from, and then pops your stashed changes onto it. Cleaning up your stash If you decide you no longer need a particular stash, you can delete it with git stash drop. For example : git stash drop stash@{1}. Or you can delete all of your stashes with: git stash clear
by Tinatin Kvinikadze
2 years ago
Git
Git stash
0
Pro tip: use ```triple backticks around text``` to write in code fences