shellstreak

git stash: save unfinished work for later

Updated · Every command on this page ran in our practice shell

Short answer

git stash saves your uncommitted changes and cleans your working tree; git stash push -m "label" names the entry. git stash list shows entries, git stash pop brings the newest back and removes it, and git stash apply brings it back but keeps it. New untracked files aren't stashed unless you add -u.

You're halfway through an edit and need a clean working tree right now: to switch branches, pull, or make a quick fix. git stash takes your uncommitted changes, puts them on a shelf, and resets your files to the last commit. Later you take them back. Every example below ran in a practice shell on a small recipes repository.

Stash your changes

git stash (short for git stash push) saves your uncommitted changes to tracked files and leaves the working tree clean. The entry is described with the branch and the commit you were on.

example
learner@shellstreak:~/recipes$ git stash && git status
Saved working directory and index state WIP on main: 7a76ad2 Add muffin recipe
On branch main
nothing to commit, working tree clean

Give the stash a name with -m

WIP on main gets confusing once you have three of them. git stash push -m "label" stores a description you'll recognise later.

example
learner@shellstreak:~/recipes$ git stash push -m "half-done bread" && git stash list
Saved working directory and index state On main: half-done bread
stash@{0}: On main: half-done bread

List your stashes

git stash list shows every entry, newest first. The newest is always stash@{0}; each new stash pushes the others down one number.

example
learner@shellstreak:~/recipes$ git stash list
stash@{0}: On main: sweeter scones
stash@{1}: On main: half-done bread

Look inside a stash

git stash show summarises which files an entry changes; add -p to see the full diff. Name an older entry with git stash show -p stash@{1}.

example
learner@shellstreak:~/recipes$ git stash show -p
diff --git a/bread.txt b/bread.txt
index 529b237..8932487 100644
--- a/bread.txt
+++ b/bread.txt
@@ -3,3 +3,4 @@ flour 500g
 water 350g
 salt 10g
 Bake at 230C
+Rest 1 hour before baking

Bring it back: pop vs apply

git stash pop re-applies the newest entry and removes it from the list. git stash apply re-applies it but keeps the entry, which is handy if you want the same changes on two branches. Both take an entry name, like git stash pop stash@{1}.

example
learner@shellstreak:~/recipes$ git stash pop
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   bread.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (290f8915e8482f374f86539a7b515c6bef4d91b8)

Apply an older entry

Give the entry's name to pick one that isn't the newest. Here stash@{1} is the older bread edit; the scones entry stays on the shelf.

example
learner@shellstreak:~/recipes$ git stash apply stash@{1} && git stash list
On branch main
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
	modified:   bread.txt

no changes added to commit (use "git add" and/or "git commit -a")
stash@{0}: On main: sweeter scones
stash@{1}: On main: half-done bread

Drop and clear

git stash drop stash@{1} deletes one entry; git stash clear deletes them all. Dropped stashes are very hard to get back, so git stash show -p first if you're not sure what's in one.

example
learner@shellstreak:~/recipes$ git stash drop stash@{1} && git stash list
Dropped stash@{1} (290f8915e8482f374f86539a7b515c6bef4d91b8)
stash@{0}: On main: sweeter scones

New files are not stashed

By default, git stash only saves changes to files git already tracks. A brand-new, untracked file stays in your folder. In real git, git stash -u (--include-untracked) stashes those too; the practice shell doesn't support -u, so there you'd git add the file first.

example
learner@shellstreak:~/recipes$ touch draft.txt && git stash && git status -s
Saved working directory and index state WIP on main: 7a76ad2 Add muffin recipe
?? draft.txt

Check yourself

One real exercise from the “Git history & undo” unit. No account needed.

Put your unfinished edit to bread.txt on the stash, so the working tree is clean.

What's checked: the repository's stash, unstaged changes.

Common mistakes

  • Using the stash as long-term storage. Entries pile up and lose their context; commit work on a branch instead.

  • Running git stash pop on the wrong branch. Check git branch first: the changes apply wherever you are.

  • Expecting new files to be stashed. Untracked files stay put unless you use -u (real git) or git add them first.

  • Running git stash clear to tidy up without looking. It deletes every entry.

Questions

What's the difference between git stash pop and git stash apply?+

Both put the changes back in your files. `pop` then deletes the entry from the stash; `apply` keeps it, so you can apply it again elsewhere.

What happens if git stash pop conflicts?+

Git applies what it can and marks conflicts like a merge. Fix the files and `git add` them. With a conflict, real git keeps the entry on the list, so `git stash drop` it once you're done.

Is git stash the same as a commit?+

Under the hood a stash is stored as special commits, but it isn't on any branch and doesn't show in `git log`. It's meant for minutes or hours, not for keeping work.

Reading is step one. Now make it stick.

Practise this in a five-minute lesson with instant feedback. No account needed.

Practise it now