Git Stash
Git stash lets you set aside uncommitted changes temporarily so you can switch context, like checking out another branch, without committing half-finished work.
Why Stash Instead of Commit?
Sometimes you need to switch branches urgently, a teammate needs a hotfix reviewed, or you spot a bug on main, but your working directory is full of changes that aren't ready to be committed. Git refuses to switch branches if doing so would overwrite modified tracked files, and committing broken, half-done work just to switch branches pollutes your history. git stash solves this by saving your working directory and index state onto a stack, then resetting your working directory to match HEAD.
Stashing Changes
Running git stash, or its explicit form git stash push, records your tracked, modified files and any staged changes. By default, untracked and ignored files are left alone; pass -u to include untracked files, or -a for absolutely everything, including ignored ones.
Stash your working changes with a message
$ git status
$ git stash push -m "WIP: pagination edge case"Viewing and Applying Stashes
Stashes are kept as a stack, most recent first, and each gets a reference like stash@{0}. git stash list shows everything you've saved. git stash apply reapplies a stash's changes to your working directory but leaves it on the stack; git stash pop does the same and then removes it from the stack, which is what most people reach for by default.
List the stash stack
$ git stash listReapply and remove the most recent stash
$ git stash popInspecting and Cleaning Up
Before applying, you can peek at what a stash contains without touching your working directory, using git stash show for a summary or with -p for the full diff. When a stash is no longer needed, remove it with git stash drop, or wipe the whole stack with git stash clear.
- git stash show -p stash@{0} - full diff of a specific stash
- git stash apply stash@{1} - apply an older stash without popping the top one
- git stash branch new-branch stash@{0} - build a branch from a stash, useful after conflicts
- git stash drop stash@{0} - delete a single stash entry
- git stash clear - delete the entire stash stack, irreversibly
Exercise: Git Stash
What does `git stash` do to your uncommitted changes?