More Git answers

by Danny
Published 2 min read

Use git stash to put unfinished changes aside, switch branches, and bring them back later. Covers naming stashes, apply versus pop, and untracked files.

The short answer

#

git stash saves your uncommitted changes and gives you a clean working tree, so you can switch branches or pull. Give the stash a name so you can find it later, include untracked files if you have any, and bring it back with git stash apply or git stash pop.


_10
git stash push -u -m "half-done header refactor"
_10
git switch main
_10
# ...do something else...
_10
git switch feature/header
_10
git stash pop

Why it happens

#

Git will not let you switch branches if your uncommitted changes would be overwritten by the switch. Sometimes you are halfway through something and not ready to commit it: an urgent fix comes in, or you need to pull and the pull conflicts with local edits.

A stash is really a set of hidden commits that record your staged and unstaged changes. Git keeps them in a stack, newest first, at stash@{0}, stash@{1} and so on. They are local only; stashes are never pushed.

How to fix it

#

Save with a message


_10
git stash push -m "wip: retry logic"

A bare git stash works too, but the automatic message only names the branch and the last commit. After a few stashes, a list of identical messages is hard to tell apart.

Include untracked files

By default, stash only saves changes to files Git already tracks. New files you have not added stay in the working tree. Add -u to include them:


_10
git stash push -u -m "new component, not yet added"

-a (--all) also includes ignored files, which you rarely want because it picks up things like node_modules and build output.

Stash only some files

Pass paths after -- to stash just those files:


_10
git stash push -m "config experiment" -- src/config.ts

See what you have


_10
git stash list # all stashes, newest first
_10
git stash show -p stash@{1} # the full diff of one stash

Apply versus pop

Both put the changes back into your working tree. The difference is what happens to the stash:

  • git stash apply restores the changes and keeps the stash in the list.
  • git stash pop restores the changes and then deletes the stash, but only if it applied cleanly.

_10
git stash apply stash@{1}
_10
git stash drop stash@{1} # remove it once you are happy

Use apply when you are not sure it will go smoothly, or when you want to apply the same changes to more than one branch. If a pop hits a conflict, Git keeps the stash so nothing is lost. Resolve the conflict as you would for a merge, then drop the stash yourself.

Restore the staged state too

By default, everything comes back unstaged. If you had carefully staged part of your work, add --index to restore the staging area as well:


_10
git stash pop --index

Turn a stash into a branch

If the branch has moved on and the stash no longer applies cleanly, create a new branch from the commit the stash was made on:


_10
git stash branch feature/retry stash@{0}

Common pitfalls

#
  • Forgetting -u and leaving new files behind, then wondering why they appear on the other branch.
  • Building up a long list of unnamed stashes. Use -m, and clear out old ones with git stash drop.
  • Running git stash clear without checking the list. It deletes every stash.
  • Treating the stash as long-term storage. A work-in-progress commit on its own branch is easier to find, can be pushed as a backup, and shows up in normal history tools.
  • Relying on a stash index such as stash@{1} in a script. The numbers shift every time anyone pushes or drops a stash in that repository, including from other worktrees, which share the same stash list.
  • Losing a dropped stash. It can often be recovered for a while with git fsck --unreachable, but do not count on it.
#

Was this resource helpful?