Git

Stashing

Git · 9 entries

Stash Changes

syntax
git stash [push] [-m "message"]
example
git stash
git stash push -m "WIP: payment form validation"
output
Saved working directory and index state WIP on main: a1b2c3d Add auth

Note Stash saves your uncommitted changes (staged and unstaged) and reverts the working directory to HEAD. New untracked files are NOT stashed by default — use -u to include them.

Stash Including Untracked Files

syntax
git stash push -u [-m "message"]
git stash push --include-untracked
example
git stash push -u -m "WIP: new config files"
output
Saved working directory and index state On main: WIP: new config files

Note Without -u, brand new files that Git hasn't started tracking will be left behind when you stash. This catches most people off guard.

Restore Stashed Changes

syntax
git stash pop [stash@{n}]
example
# Restore most recent stash and remove it from the stash list:
git stash pop

# Restore a specific stash:
git stash pop stash@{2}
output
On branch main
Changes not staged for commit:
  modified:   src/cart.js

Note pop = apply + drop in one step. If there's a conflict during pop, the stash is NOT dropped — you'll need to resolve the conflict and manually run git stash drop afterwards.

Apply Stash Without Removing

syntax
git stash apply [stash@{n}]
example
git stash apply
git stash apply stash@{1}

Note Unlike pop, apply keeps the stash in the list. Useful when you want to apply the same stash to multiple branches.

List All Stashes

syntax
git stash list
example
git stash list
output
stash@{0}: On main: WIP: payment form validation
stash@{1}: WIP on feature/auth: a1b2c3d Add login
stash@{2}: On main: WIP: new config files

Note Stashes are stored as a stack — stash@{0} is always the most recent. The list shows which branch and commit each stash was created from.

Delete a Stash

syntax
git stash drop [stash@{n}]
git stash clear
example
# Drop a specific stash:
git stash drop stash@{1}

# Delete ALL stashes:
git stash clear
output
Dropped stash@{1} (a1b2c3d4e5f6...)

Note WARNING: git stash clear deletes every stash with no confirmation and no way to recover them. Drop individual stashes if you're not sure.

View Stash Contents

syntax
git stash show [-p] [stash@{n}]
example
# Summary of changes:
git stash show stash@{0}

# Full diff:
git stash show -p stash@{0}
output
 src/cart.js | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

Note Without -p, you see a summary like git diff --stat. With -p, you see the full diff. Useful for checking what's in a stash before applying it.

Create Branch from Stash

syntax
git stash branch <branch-name> [stash@{n}]
example
git stash branch feature/rescue-work stash@{0}
output
Switched to a new branch 'feature/rescue-work'
On branch feature/rescue-work
Changes not staged for commit:
  modified:   src/cart.js
Dropped stash@{0} (a1b2c3d...)

Note Creates a new branch starting from the commit where the stash was originally created, applies the stash, then drops it. Perfect when you stashed on the wrong branch.

Stash Specific Files

syntax
git stash push [-m "message"] -- <file1> <file2>
example
git stash push -m "just the config changes" -- config.yaml .env.example
output
Saved working directory and index state On main: just the config changes

Note The -- separator tells Git that everything after it is a file path. You can also use git stash push -p to interactively choose which hunks to stash.