Git & GitHub · 9 min · 110 XP

Set work aside with stash

Set unfinished work aside with stash, and bring it back without losing it.

You're halfway through an edit and suddenly need a clean working copy: to pull a teammate's fix, to switch branches and review something, or to chase an urgent bug. Committing half-done work is one option. git stash is the other. It takes your uncommitted changes, staged and unstaged, puts them to one side, and leaves your files exactly as the last commit had them.

bash
git stash          # set aside changes to tracked files
git stash -u       # ...and new, untracked files too
git stash list     # stash@{0}: WIP on main: c000892 Add cart
git stash pop      # bring the latest back and remove it from the list
git stash apply    # bring it back but keep it in the list

Two things catch people out. A plain git stash leaves new, untracked files where they are, so add -u to take those too. And the stash lives only on your machine. It's never pushed, so it's a place to park work for an hour, not a backup.

Bringing a stash back is a small merge, so it can conflict with what changed in the meantime. When pop hits a conflict, git keeps the stash in the list instead of dropping it. Resolve the file as you would any conflict, then remove the stash yourself with git stash drop.

Scenario: you pull with an edit in progress
$ git pull
Updating c000892..01b6abb
error: Your local changes to the following files would be overwritten by merge:
	cart.js
Please commit your changes or stash them before you merge.
Aborting
Scenario: right after stashing
$ git stash
Saved working directory and index state WIP on main: c000892 Add cart
$ git status --short
?? notes.txt
Scenario: bringing the stash back after the pull
$ git stash pop
Auto-merging cart.js
CONFLICT (content): Merge conflict in cart.js
...
The stash entry is kept in case you need it again.

Try it on your machine: in your practice repository, edit a file and create a new one. Run git stash and check git status: the new file is still there. Run git stash pop, then try git stash -u and see both disappear. Bring them back with git stash pop.

Loading your workspace…