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.mdmodifiedRepository
10 commits on main
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add adobo recipe
Stash, fix, pop
- Put all the unfinished work aside, new file included
- Change
10 egg yolksto12 egg yolksin leche-flan.md and commit only that - Bring the unfinished work back and check it’s all there
Working folder
Staging area
pancit.mdmodifiedRepository
10 commits on main
You are in ~/kusina. Some changes are staged, some aren’t, and halo-halo.md is new.
Commit graph
67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add 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
| Command | Does |
|---|---|
git stash or git stash push | put tracked changes aside |
git stash -u | also untracked files |
git stash push -m "halo-halo draft" | put aside with a name |
git stash list | list stashes: stash@{0} is the newest |
git stash pop | apply the newest, then drop it |
git stash apply | apply 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
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
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.
- Git documentation: git-stash: push, list, pop, apply, drop, -u and -m
- Pro Git: Stashing and Cleaning: stashing work in progress