Gitastic
Path › Part 4: Undo & rescue › Lesson 12 of 16
12

Stash: the pocket

Park half-done work, switch context, pick it back up.

1 Learn the idea

You are halfway through a change when someone says "quick, fix production!". Your work is not ready to commit, and git won't let you switch branches if that would overwrite your edits. git stash handles this.

  • git stash: save tracked changes (staged and unstaged) and clean the working tree.
  • git stash -u: also include untracked (new) files, which a plain stash leaves behind.
  • git stash -m "note": add a label so you remember what it was.
  • git stash list: stashes form a stack. stash@{0} is the newest.
  • git stash pop: reapply the newest stash and remove it. git stash apply reapplies but keeps it.

2 Watch it happen

Interrupted mid-dessert

Press Play or use → to step through. Watch the graph as each command runs.

2f5672f Add menu2f5672fAdd menu5e7b713 Add salad5e7b713Add saladBranch "main" — a movable pointermainHEAD = where you are. It points at the current branch.HEAD →Branch "salad" — a movable pointer (current branch: moves when you commit)salad
Working directoryyour folder / editor
menu.md
git add →← git restore
Staging areathe next snapshot (index)
menu.md
git commit →← restore --staged
RepositoryHEAD → 5e7b713
menu.md
Stash
empty
terminal · simulated, no real repository is touched
Read this demo as text
  1. $ edit menu.md "Soup, Salad, Tiramisu (half-written…)"

    We are on salad, midway through an edit. It is not ready to commit.

  2. $ git switch main

    An urgent fix is needed on main. The switch is refused: main has a different menu.md, and switching would overwrite our edit.

  3. $ git stash

    The change goes into the stash and the working tree is clean again. The stash remembers which commit it was based on.

  4. $ git stash list

    Stashes form a stack. stash@{0} is the newest one. Its default label says where it was made.

  5. $ git switch main

    The working tree is clean, so switching is allowed now.

  6. $ echo "Open 9-17" > hours.md
    $ git add hours.md
    $ git commit -m "Add opening hours"

    We make the urgent fix and commit it on main.

  7. $ git switch salad

    Back on our feature branch.

  8. $ git stash pop

    pop puts the half-written tiramisu back into the working directory and removes the entry from the stash.

  9. $ echo "idea: vegan menu" > ideas.txt
    $ git stash -u -m "tiramisu + ideas"

    ideas.txt is a new, untracked file, and a plain stash would leave it behind. -u includes untracked files. -m gives the stash a readable label.

  10. $ git stash list

    The labelled stash waits in the pocket. git stash pop would bring back both files.

git stash [-u] [-m "note"]Pocket uncommitted changes
git stash listSee what is in the pocket
git stash popReapply newest and remove it
git stash apply / dropReapply but keep it / throw one away

3 Check your understanding

Question 1

You created a brand new file draft.md (never added) and run plain git stash. What happens to draft.md?

Question 2

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

Question 3

Look at the graph. What does the stash@{0} label mark?

2f5672f Add menu2f5672fAdd menue375f09 Add pricese375f09Add pricesHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)mainA stash entry was created on top of this commitstash@{0}
Stash
stash@{0} WIP on main: e375f09 Add prices
Question 4

Your changes will need a few days of on-and-off work. Best place to keep them?

4 Practice for real

Your mission

Hotfix without losing your place

You are mid-edit on salad. Commit a new file hours.md on main (message must mention "hours"). Then return to salad and get your unfinished menu.md edit ("Soup, Salad, Dressing") back, with the stash empty.

2f5672f Add menu click to insert the hash into the terminal2f5672fAdd menuc389a33 Start salad click to insert the hash into the terminalc389a33Start saladBranch "main" — a movable pointermainHEAD = where you are. It points at the current branch.HEAD →Branch "salad" — a movable pointer (current branch: moves when you commit)salad
Working directoryyour folder / editor
menu.mdchanged
git add →← git restore
Staging areathe next snapshot (index)
menu.md
git commit →← restore --staged
RepositoryHEAD → c389a33
menu.md
Stash
empty
terminal · simulated, no real repository is touched
Type commands below. `help` lists everything. Tab completes, ↑ recalls. Click a commit to paste its hash.
~/project (salad) $