git stash

Branching & Merging

Stash the changes in a dirty working directory away


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. You can re-apply the stashed changes later with 'git stash pop' or 'git stash apply'. Stashing is useful when you need to quickly switch context and work on something else, but you're mid-way through a change and aren't ready to commit. The stash is stored as a stack, so you can have multiple stashes.

Synopsis

git stash list [<log-options>]
git stash show [-u | --include-untracked | --only-untracked] [<diff-options>] [<stash>]
git stash drop [-q | --quiet] [<stash>]
git stash pop [--index] [-q | --quiet] [<stash>]
git stash apply [--index] [-q | --quiet] [<stash>]
git stash branch <branchname> [<stash>]
git stash [push [-p | --patch] [-S | --staged] [-k | --[no-]keep-index] [-q | --quiet] [-u | --include-untracked] [-a | --all] [(-m | --message) <message>] [--] [<pathspec>...]]
git stash clear
[ ] optional| or<arg> placeholder... repeatable--flag option

Options

Click any option to expand

Beginner (7)

Save your local modifications to a new stash entry and roll them back to HEAD. Use -m to add a descriptive message. This is the default action when running 'git stash'.

RecommendedUse with -m so every stash is identifiable later

Example

git stash push -m "Work in progress"

Apply the most recent stash (stash@{0}) to your working directory and remove it from the stash stack. If applying hits conflicts, the stash entry is kept so you can resolve and drop it yourself.

RecommendedRestores and removes the latest stash; kept if applying conflicts

Example

git stash pop

Apply a stash without removing it from the stack. Unlike pop, the stash remains available for future use. Can specify which stash with stash@{n}.

When neededWhen you need the same stash again or on several branches

Example

git stash apply stash@{1}

List all stash entries. Each entry shows its index (stash@{n}), the branch it was created on, and the message or commit description.

RecommendedCheck what is stashed before popping or dropping

Example

git stash list

Show the changes recorded in a stash entry as a diff. By default shows stash@{0}. Add -p for a full patch view.

RecommendedPreview with -p before applying

Example

git stash show -p stash@{0}

Remove a single stash entry from the stack. Without arguments, drops stash@{0}. Specify stash@{n} to drop a specific stash. Note the printed hash - it is the only easy way to recover it.

When neededClean up after apply; note the printed hash in case you need it back

Example

git stash drop stash@{1}

Remove ALL stash entries at once, with no confirmation. Recovery is only possible by hunting dangling commits with git fsck. Use with extreme caution.

Not recommendedDeletes every stash at once, with no confirmation

Example

git stash clear

Intermediate (3)

Include untracked files in the stash. By default, git stash only saves tracked files. Use this to also stash new files that haven't been added yet.

RecommendedWithout it, new untracked files are left behind

Example

git stash -u

Keep the staging area intact. Only unstaged changes are removed from the working directory; staged changes remain staged (the stash still records them too).

When neededTest or commit staged work while setting the rest aside

Example

git stash -k

Create a new branch from the commit where the stash was created, apply the stash, and drop it if successful. Useful when stash conflicts with current branch.

When neededBest fix when a stash no longer applies cleanly to the current branch

Example

git stash branch new-feature stash@{0}

Advanced (1)

Interactively select hunks to stash. Allows you to stash only specific parts of your changes while keeping others in the working directory.

When neededStash only selected hunks

Example

git stash -p

Choosing the Right Option

Restoring Stashed Changes

Choose the right command to restore your stashed work

CommandKeeps Stash?Applies ToOn Conflict
git stash popNo - removes after applyCurrent branchStash kept, must resolve
git stash applyYes - stays in stackCurrent branchStash kept, must resolve
git stash branch <name>No - removes after applyNew branch at stash baseNo conflicts (fresh branch)

What Gets Stashed?

Control which files are included in your stash

CommandTracked ModifiedStagedUntrackedIgnored
git stashYesYesNoNo
git stash -uYesYesYesNo
git stash -aYesYesYesYes
git stash -kYesYes (and also kept staged)NoNo

Stash vs Other Save Methods

When to use stash versus alternatives

CommandPersists WhereShareableBest For
git stashLocal reflogNoQuick context switch, temporary save
git commit (WIP)Branch historyYes (if pushed)Work you might want to share or reference
git worktreeSeparate directoryN/AWorking on multiple branches simultaneously

Managing Multiple Stashes

Commands for working with the stash stack

CommandShows ContentModifies StackUse Case
git stash listIndex + messageNoSee all stashes
git stash showDiffstatNoQuick preview of changes
git stash show -pFull diffNoDetailed review before apply
git stash dropNoYes (removes one)Delete specific stash
git stash clearNoYes (removes ALL)Start fresh (dangerous)

Examples

Stash current changes

git stash

Saves all uncommitted changes (staged and unstaged) to the stash stack and reverts working directory to HEAD.


Stash with a descriptive message

git stash push -m "WIP: login feature"

Stashes changes with a custom message for easier identification when you have multiple stashes.


Apply and remove latest stash

git stash pop

Applies the most recent stash (stash@{0}) to your working directory and removes it from the stash stack.


List all stashes

git stash list

Shows all stash entries with their index, branch name, and message. Useful when managing multiple stashes.

Workflow Examples

1
git stash

Save current changes to stash

2
git checkout other-branch

Switch to the other branch

3
(do work, commit if needed)

Complete the urgent work

4
git checkout original-branch

Return to your branch

5
git stash pop

Restore your stashed changes

1
git stash push -m "WIP: login validation"

Stash with descriptive message

2
(days later...)

Time passes

3
git stash list

See all stashes with messages

4
git stash show stash@{0}

Preview what's in the stash

5
git stash pop

Apply and remove when ready

1
git stash

Stash your changes

2
git checkout branch-a

Switch to first branch

3
git stash apply

Apply (keeps stash)

4
git commit -m "Add feature"

Commit on branch-a

5
git checkout branch-b

Switch to second branch

6
git stash apply

Apply same stash again

7
git stash drop

Clean up when done

1
git add ready-file.js

Stage the ready changes

2
git stash -k

Stash unstaged, keep staged

3
git commit -m "Ready feature"

Commit the staged changes

4
git stash pop

Restore the rest of your work

Troubleshooting

error: Your local changes to the following files would be overwritten by merge

Cause: Trying to pop/apply a stash that conflicts with current working directory

Either commit/stash current changes first, or use 'git stash branch <name>' to create a new branch from the stash

No stash entries found

Cause: The stash stack is empty or you're looking at the wrong index

Run 'git stash list' to see available stashes. Remember stash@{0} is most recent.

Stash didn't save my new files

Cause: By default, git stash only saves tracked files

Use 'git stash -u' to include untracked files, or 'git stash -a' to include ignored files too

Applied wrong stash / want to undo pop

Cause: Accidentally popped the wrong stash entry

If pop failed with conflicts, stash is still there. If successful, your changes are in working directory - just 'git stash' again if needed

Lost stash after clear/drop

Cause: git stash clear/drop permanently removes stash entries

Try 'git fsck --unreachable | grep commit' then 'git show <hash>' to find lost stash commits. Recovery not guaranteed.

Tips & Pitfalls

Best Practices

  • ✓Always use descriptive messages with 'git stash push -m "description"' - you'll thank yourself later
  • ✓Prefer 'git stash apply' over 'git stash pop' when you might need the stash again
  • ✓Use 'git stash -u' if you have new untracked files you want to save
  • ✓Don't let stashes pile up - regularly clean old stashes with 'git stash drop'
  • ✓Consider creating a WIP commit instead of stashing for long-term saves
  • ✓Use 'git stash show -p' to review stash contents before applying
  • ✓When stash pop fails due to conflicts, the stash is preserved - resolve conflicts then drop manually

Common Mistakes

  • ✗Forgetting that 'git stash' only stores tracked files by default. Use '-u' or '--include-untracked' to stash new untracked files.
  • ✗Using 'git stash pop' when you might want to apply the same stash to multiple branches. Use 'git stash apply' instead to keep the stash.
  • ✗Not giving stashes descriptive messages and then forgetting what each stash contains. Always use 'git stash push -m "description"'.
  • ✗Running 'git stash clear' without realizing it deletes ALL stashes without confirmation. Recovery is only possible by digging dangling commits out with 'git fsck'.
  • ✗Confusing the stash stack order: stash@{0} is the MOST recent stash, not the oldest.

FAQ

A:

Pop applies the stash AND removes it from the stack. Apply only applies it, keeping the stash available. Use apply when you might need the same stash elsewhere.

A:

By default, stash only saves tracked files. Use 'git stash -u' to include untracked files, or 'git stash -a' to also include ignored files.

A:

Use 'git stash show' for a quick summary, or 'git stash show -p' for the full diff. Add stash@{n} to view a specific stash.

A:

Yes! Use 'git stash push -m "message" -- file1.js file2.js' to stash only specific files, or 'git stash -p' to interactively select hunks.

A:

The stash is NOT removed when conflicts occur. Resolve the conflicts manually, then use 'git stash drop' to remove the stash when done.

A:

Stashes are branch-independent. Just checkout the target branch and run 'git stash apply'. Or use 'git stash branch <name>' to create a new branch.