Looking back and undoing · lesson 6 of 7
Commands and ideas in this lesson
Agit stashput changes aside
Bgit stash popbring them back and drop the stash
Cgit stash listwhat's in the drawer
D-u, -minclude new files; give it a name

Putting work aside

Last updated

Halfway through a change, something more urgent comes up. git stash puts your unfinished work in a drawer and gives you a clean folder; git stash pop puts it back. In this lesson you'll use the stash, name what you put in it, and see when a branch would be better.

By the end of this lesson you can

  • Put uncommitted work aside with git stash, including new files with -u
  • List, apply and pop stashes
  • Name a stash so you can find it later

The drawer

Ana is in the middle of changes to the adobo and pancit, and has started a halo-halo recipe. Lola Remy calls: the leche flan has 10 egg yolks in it, and it should be 12, and Carla is making it tonight. Ana needs to fix that now, in a commit of its own, without her half-done work getting mixed in.

git stash lifts every uncommitted change off the working folder and the staging area and keeps it in a stash, leaving the folder clean, as at the last commit. Later, git stash pop puts the changes back.

$ git status -s
 M adobo.md
M  pancit.md
?? halo-halo.md

Working folder

Staging area

pancit.mdmodified

Repository

10 commits on main

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe
1 of 4 Half-done work: two changed recipes and a new file.

Stash, fix, pop

Git sandbox
  1. Put all the unfinished work aside, new file included
  2. Change 10 egg yolks to 12 egg yolks in leche-flan.md and commit only that
  3. Bring the unfinished work back and check it’s all there

Working folder

Staging area

pancit.mdmodified

Repository

10 commits on main

You are in ~/kusina. Some changes are staged, some aren’t, and halo-halo.md is new.

Commit graph

  1. 67e435d HEAD →main Cover the leche flan with foil
  2. ba04ca2 Add lumpiang shanghai recipe
  3. 98a5edc Add a script that builds the index page
  4. d078487 Make the sinigang steps clearer
  5. 145dc1e Add pancit bihon recipe
  6. 52d1f4d Say how much salt goes in the adobo
  7. 1c17fad Add a README
  8. 9c53888 Add leche flan recipe
  9. 1f359a2 Add sinigang recipe
  10. 601c654 Add adobo recipe

Use edit leche-flan.md to make the change. Notice that after stash pop, the pancit change is back but no longer staged: pop brings back the changes, not what was staged. git stash pop --index would restore that too.

Apply, pop and drop

CommandDoes
git stash or git stash pushput tracked changes aside
git stash -ualso untracked files
git stash push -m "halo-halo draft"put aside with a name
git stash listlist stashes: stash@{0} is the newest
git stash popapply the newest, then drop it
git stash applyapply it, and keep it in the stash too
git stash drop stash@{1}throw one away

If popping a stash clashes with changes you've made since, Git stops with a conflict and keeps the stash, so nothing is lost. Lesson 3.4 shows how to settle conflicts.

git stashCareful: changes files or history, but Git can usually get it back keeps your work safe. git stash dropCan lose work: can throw away work Git never saved, or rewrite shared history throws a stash away.

Spot the trap: a stash forgotten for three months

Spot the trapAna

In March, Ana stashed a half-typed kare-kare recipe to fix something else, and never popped it. In June she can’t find the kare-kare anywhere. git stash list shows: stash@{0}: WIP on main: 52d1f4d Say how much salt… and three more like it.

What happens if…

Check what you learned

What does git stash pop do that git stash apply doesn’t?
Ana stashes with plain git stash. Her new, never-added file halo-halo.md…
Work you’ll come back to next week: stash or branch?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git