Skip to content

Chapter 5 of 8

Stashing Work in Progress

git stash provides a way to set work aside without committing it, useful when you need a clean working tree to switch branches, pull, or bisect. git stash push -m 'message' saves the current modifications onto a stack of stashes and resets the working tree. By default only tracked files are stashed; git stash push -u (or --include-untracked) folds in untracked files as well, which is handy when switching contexts to a new branch where you want a truly clean slate.

To find your way back to stashed work, git stash list shows every entry with its message and a stash reference such as stash@{0} for the most recent. git stash pop reapplies the most recent stash and removes it from the stack, while git stash apply reapplies it but keeps the entry for possible reuse; both can target a specific entry by reference, as in git stash apply stash@{2}. Removing entries without applying is done with git stash drop stash@{0} for one, and git stash clear to wipe the entire stack, which is destructive and should be used with care.

All chapters
  1. 1Undoing Commits and Local Changes
  2. 2Cleaning the Working Tree and Fixing .gitignore
  3. 3Rewriting History with Rebase
  4. 4Merging and Conflict Resolution
  5. 5Stashing Work in Progress
  6. 6Branches, Remotes, Tags, and Force-Push Safety
  7. 7Investigating and Searching History
  8. 8Advanced Recovery, Configuration, and Housekeeping

Drill it

Reading is not remembering. These come from the Git Recovering From Common Mistakes deck:

Q

Undo the last commit but keep changes staged.

git reset --soft HEAD~1

Q

Undo the last commit AND discard changes.

git reset --hard HEAD~1  ⚠ destructive.

Q

Undo the last commit but keep changes unstaged in the working tree.

git reset --mixed HEAD~1 (the default).

Q

Amend the last commit message.

git commit --amend -m 'new message'