Git - stashing

Git stash

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.

The stash is local to your Git repository; stashes are not transferred to the server when you push.

Creating and viewing stashes

command description
git stash uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy
git stash save "message" Same as above - but gives a recognizable name to the stash
git stash pop removes the changes from your stash and reapplies them to your working copy
git stash pop stash@{2} pop a stash with a specific name. default behavior of pop is to pick the most recent stash.
git stash apply reapply the changes to your working copy and keep them in your stash. useful if you want to apply the same stashed changes to multiple branches
git stash -u (or --include-untracked) to stash untracked or ignored files
git stash -a (or --all) include changes to ignored files in the stash
git stash list see list of all stashes in local

Viewing stash diffs

command description
git stash show see a summary of the stash
git stash show -p (or --patch) view the full diff of a stash

Creating a branch from stash

command description
git stash branch feature/branch-name stash@{1} see a summary of the stash

Cleaning up stash

command description
git stash drop stash@{1} Drop a specifis stash by name
git stash clear delete all of your stashes

Reading material

  1. https://www.atlassian.com/git/tutorials/saving-changes/git-stash

Links to this note