Stash and restore changes
Shelves uncommitted changes, including untracked files, onto a labeled stack so you can switch branches with a clean working tree.
Last updated
git stash push -u -m "wip"
How it works
git stash push saves every uncommitted change to tracked files (staged and unstaged alike) onto a stack and resets the working tree to match HEAD, freeing you to switch branches, pull, or check out something else without committing half-finished work. -u extends that to untracked files too, which plain git stash otherwise skips entirely — easy to forget and lose track of. -m "wip" attaches a readable label instead of the generic "WIP on branch" name every unlabeled stash gets, which becomes impossible to tell apart once you have more than one.
To bring the changes back, run git stash pop — it reapplies the most recent stash and removes it from the stack in the same step, restoring the working tree to exactly where you left off.
Watch out for
- →git stash pop can conflict, just like a merge, if the working tree has changed since you stashed — resolve the conflicts by hand, and note the stash entry stays on the stack until you manually git stash drop it (pop only removes it automatically on a clean, conflict-free apply).
- →Use git stash apply instead of pop if you want to reapply the same stash to more than one branch, since apply leaves it on the stack afterward instead of consuming it.
- →Stashes live only in your local clone — they're never pushed, fetched, or visible to collaborators — and git stash clear deletes every stash at once with no confirmation, so check git stash list first if you're not sure what's on the stack.
Part by part
- git stash push
- The modern form. Plain git stash still works, but push accepts the options below and reads consistently with the rest of the command set.
- -u
- Include untracked files. Without it a new file you have not added stays in the working tree, which is the single most common stash surprise.
- -m "wip"
- A message, so the stash list is readable. Unlabelled stashes are identified only by the commit they were made from, which tells you nothing a week later.
Common mistakes
git stashLeaves untracked files behind, so switching branches afterwards carries a half-finished new file with you — and it is easy to conclude the stash simply did not work.
git stash push -u -m "wip"git stash popPop removes the stash as it applies it, so a conflict during the apply can leave you with neither a clean tree nor a stash entry. git stash apply keeps the entry until you are satisfied.
git stash clearDeletes every stash with no confirmation and no undo through normal means. If you meant to remove one, git stash drop takes a specific reference.
Questions
How do I get my changes back?
git stash apply restores the most recent entry and keeps it in the list; git stash pop does the same and removes it. Apply is the safer habit, with an explicit drop once you have confirmed the result.
How do I see what is in a stash before applying it?
git stash list names them, and git stash show -p stash@{0} prints the full diff. Worth doing when the list has grown, since applying the wrong one produces a confusing mixture.
Does a stash survive a branch switch?
Yes — stashes are stored per repository, not per branch, so you can stash on one branch and apply on another. That is a feature, and it is also how changes end up somewhere unexpected.
Is a stash included in a backup or a push?
No. Stashes are local refs and are never pushed, so anything left in one exists on that machine alone. Long-lived work belongs on a branch, not in a stash.