Advanced

Pigeonholing uncommitted work

A stash is git's temporary coat check. I reached for it the day a boss message interrupted a half-finished commit and I needed my working folder spotless in two seconds.

What you will see

Dropped refs/stash@{0} (abc4567) M app.py wip-a

About 2 min of reading

Quick check

Before this new lesson, one small question about the lesson you just finished. Recall it, do not look it up.

Try It

Put my half-finished work in git's temporary coat check so I could leave my folder clean, then bring the same change back later.

1

Start from a clean commit, then edit a tracked file so there is uncommitted work waiting. git status --short shows the file as modified right before I hide it.

printf 'v1\n' > app.py git add app.py git commit -m initial printf 'wip-a\n' > app.py git status --short

Before you run it, say out loud what it will print. Then open the answer.

What it prints
M app.py
Make sure the file is already committed once before you change it again, otherwise there is nothing to stash against.
2

Hide the change with the plain stash. Git reverts your working folder to the last commit and stores the change on a hidden stack instead of deleting it.

git stash git status --short

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Saved working directory and index state WIP on main: abc4567
The empty status line after it means the folder is clean again. If your command does not say "Saved working directory", git had nothing to stash.
3

Look at the stash stack to confirm your work is waiting. Each stash is numbered from the newest; stash@{0} is always the most recent one.

git stash list

Before you run it, say out loud what it will print. Then open the answer.

What it prints
stash@{0}: WIP on main: abc4567
The stash stays here even after you switch branches or start other work. That stack is how you get the change back later.
4

Bring the change back with pop. It restores the newest stash on top of your current work and then drops it off the stack automatically.

git stash pop git status --short cat app.py

Before you run it, say out loud what it will print. Then open the answer.

What it prints
Dropped refs/stash@{0} (abc4567) M app.py wip-a
If `pop` reports a conflict, the saved change does not fit your current branch and git leaves the stash in place so nothing is lost. Resolve the overlap, then `git stash drop`.

Put it in order

Before you write anything, put these lines back in the right order. Click a line to move it up one step. This support step is easier than writing code from scratch, and it is here to make the last example stick.

Data Flow

Stash removes uncommitted work from your folder; pop restores it later.
1 Make uncommitted changes
↓
2 git stash hides them and cleans the folder
↓
3 Switch branches or start other work
↓
4 git stash pop brings the saved work back
↓
5 Resolve any conflict the pop surfaces
Why this works
The stash stack

Stashes pile up in order:

git stash list
# stash@{0}: WIP on main
# stash@{1}: WIP on feature

stash@{0} is the newest. pop takes the top one off; apply reaches for any of them by number. Each stash is a real commit in disguise, quietly holding your uncommitted changes.

Stashing files nobody tracks

By default stash only saves changes to files git already knows about. Add a flag to grab untracked files too:

git stash -u

-u means include untracked. Handy before you delete a throwaway file you might want again, though I always name my stashes on day one, a stash with no message gets a boring WIP on main label that is easy to misread.

Examples

Clear the table

git stash

This hides every uncommitted change and reverts your folder to the last commit, clean enough to switch branches without trouble. Your work is not gone, it waits in a hidden list, ready to be brought back.

Bring it back

git stash pop

pop restores the most recent stash on top of your current work. I once popped onto a branch that had moved on, and git shouted a conflict in my face. That is git being honest: the saved work did not cleanly fit, so it asked me to sort it out.

When there are several stashes

git stash list
git stash apply stash@{1}

list shows every stash with an index. apply brings back a specific one by that index, so you are not limited to the most recent. Think of them as a stack of saved workspaces, and you pick which to reopen.

Words this lesson uses

Each one is defined here on this site. Open the entry for the full definition and the other words it points at.

← The merge that refused to cooperate
Previous Lesson
The merge that refused to cooperate
Next Lesson
Undoing yourself on purpose
Undoing yourself on purpose →