Git Stash

git stash temporarily shelves your uncommitted changes so you get a clean working directory, without making a commit. It is perfect when you are halfway through something and suddenly need to switch branches to fix an urgent bug.

Stashing and Restoring Changes

git status --short
git stash push -m "WIP: navbar redesign"
git status --short
# ...switch branches, fix the bug, come back...
git stash pop
 M navbar.css
 M navbar.jsx
Saved working directory and index state On feature/nav: WIP: navbar redesign
(nothing — working tree clean)
On branch feature/nav
Changes not staged for commit:
        modified:   navbar.css
        modified:   navbar.jsx
Dropped refs/stash@{0}

Listing and Applying Stashes

You can have many stashes. They form a stack, with the newest as stash@{0}.

git stash list
git stash show -p stash@{1}     # view the changes
git stash apply stash@{1}       # apply but keep it in the list
git stash drop stash@{1}        # delete it
stash@{0}: On feature/nav: WIP: navbar redesign
stash@{1}: On main: experiment with fonts

pop vs apply

  • git stash pop — applies the stash and removes it from the list if it applied cleanly.
  • git stash apply — applies the stash but keeps it, so you can apply it elsewhere too.
  • If applying causes a conflict, the stash is kept; resolve the conflict, then drop it manually.

Stashing Untracked and Specific Files

git stash -u                         # include untracked (new) files
git stash push -- src/api.js         # stash only one file
git stash branch fix/fonts stash@{0} # create a branch from a stash
Saved working directory and index state WIP on main: a1b2c3d Add README

When to Use Stash

Stash is great for short interruptions. For longer pauses, a work-in-progress commit on your branch is safer, because stashes are easy to forget and are not pushed to the remote. You can always amend or squash the WIP commit later. Clear old stashes with git stash clear once you are sure you do not need them.

Frequently asked questions

What is the difference between git stash pop and git stash apply?

Both reapply stashed changes. pop then deletes the stash; apply keeps it in the stash list.

Does git stash include new files?

Not by default. Use git stash -u to include untracked files, or -a to also include ignored files.

How do I recover a dropped stash?

Run git fsck --unreachable | grep commit to find dangling commits, inspect them with git show, and apply the right one with git stash apply <hash>.