Stashing unfinished work

Intermediate · 5 min

Shelve uncommitted changes with git stash, bring them back with pop or apply, include untracked files, and use --keep-index to test only what you staged.

Before thisCreating and switching branchesUndoing uncommitted changes

Colt Steele · 5 min

Stashing unfinished work

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Colt SteeleReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • git stash saves your uncommitted changes on a stack and reverts the working directory to the last commit, so you can switch tasks.
  • git stash pop restores the newest stash and removes it, while git stash apply restores it and keeps the entry.
  • By default a stash holds changes to tracked files only. Add -u to include untracked files.
  • A plain pop restores staged changes as unstaged. Use git stash pop --index to restore the staging area too.
  • git stash push --keep-index stashes only the unstaged part, so you can test exactly what you plan to commit.

Half-finished work and an interruption

You are partway through a done command for tinynotes. The file doesn’t run yet. A bug report arrives and you need a clean working directory to fix it. You can’t commit half-finished code without polluting history, and, as the branching lesson showed, Git won’t let you switch branches if your edits would be overwritten. You also don’t want to copy the file somewhere and hope.

git stash is the shelf. It saves your uncommitted changes, resets the working directory to the last commit, and remembers the changes so you can take them back down.

The basic loop

$ git status -s
 M notes.py
?? draft.txt
$ git stash
Saved working directory and index state WIP on urgent: 3702e9b Merge branch 'fix-count'
$ git status -s
?? draft.txt

notes.py is back to its committed state, and the change is on the stash stack:

$ git stash list
stash@{0}: WIP on urgent: 3702e9b Merge branch 'fix-count'
$ git stash show -p
@@ -20,6 +20,8 @@ if __name__ == "__main__":
         show()
+    elif sys.argv[1] == "done":
+        print("TODO: mark a note done")

git stash show summarises it, and -p shows the patch. A stash is stored as commits under the hood, which is why it survives switching branches. After the urgent fix I went back to main, which is a different branch from where I stashed, and ran:

$ git stash pop
On branch main
Changes not staged for commit:
	modified:   notes.py
...
Dropped refs/stash@{0} (4ad9ebf2b568404b148e2e5f47b4e4fa3fb28f23)

The change reappeared in the working directory, and the stash entry was dropped. A stash isn’t tied to a branch. It is a patch against a commit, applied wherever you are.

pop, apply and drop

  • git stash pop restores the newest stash and deletes it if it applied cleanly.
  • git stash apply restores but keeps the entry, handy if you want the same change on several branches.
  • git stash drop stash@{1} deletes one entry without applying it.

Stashes form a stack, with stash@{0} the newest. Give them names so the list is readable:

$ git stash push -m "done command wip"
$ git stash list
stash@{0}: On main: wip with draft
stash@{1}: On main: done command wip

git stash apply stash@{1} brought the older one back and left it listed. Stashes pile up if you forget them, so check git stash list now and then.

Untracked files

By default, only changes to files Git tracks are stashed. My draft.txt was untracked, so it stayed behind after the first stash. To take it too:

$ git stash push -u -m "wip with draft"
$ git status -s
$ git stash show --include-untracked --stat stash@{0}
 draft.txt | 1 +

-u (--include-untracked) takes untracked files. Files matched by .gitignore are skipped unless you use -a. I confirmed that notes.txt, which tinynotes ignores, was untouched by a plain stash. I did not run -a.

Staged changes and --index

A plain pop restores your edits but forgets what was staged. I had a file staged and modified further (MM), stashed, popped, and got:

$ git status -s
 M notes.py

Both halves merged into one unstaged change. To restore the staging area as well, use git stash pop --index, which gave MM notes.py back in my test.

--keep-index: test what you’ll commit

This answers the worry from the add -p lesson, that a partly staged commit might not work. I staged a good change (a docstring), then added a syntax error to the working file without staging it. Then:

$ git stash push --keep-index
$ git status -s
M  notes.py

The staged change stayed in place, and the unstaged half, the broken line, went onto the stash. Now the working directory is exactly what a commit would hold. I ran a syntax check on the file and it passed. You’d run your tests the same way. Then git stash pop brings the rest back.

When pop conflicts

If the stashed change touches lines that changed since, pop can conflict, as with a merge. I tested this, and Git printed CONFLICT (content): Merge conflict in README.md and then:

The stash entry is kept in case you need it again.

That’s the safety net. You fix the conflict like any other (edit, git add), and drop the stash yourself with git stash drop once you’re sure.

Stash to a branch

When a stash turns out to deserve a branch of its own, git stash branch idea-branch creates a branch from the commit the stash was made on, applies it, and drops the entry. It worked in my test and left ## idea-branch with the change in the working directory. If there is nothing to stash, Git says so: “No local changes to save”.

What the video covers

Colt Steele’s “Git Stash In 5 Minutes” is just under five minutes and was published around 2022. I had no transcript. From the title it is a short walkthrough of the basic stash and pop loop, so the untracked, --index and --keep-index material above is probably beyond it. It is in English. The Modern Coder’s stash video is linked as a second view.

Where this fits

You can now set work aside, so a branch switch is never blocked. The last tool in this group moves a single commit from one branch to another: cherry-picking commits.

Check yourself

  1. 01You have uncommitted edits and need to switch to a branch where those files differ. What do you do?

    git stash, switch, do the other work, switch back, and git stash pop. Without the stash Git would refuse the switch.

  2. 02What is the difference between git stash pop and git stash apply?

    Both restore the changes. pop also deletes the stash entry on success, while apply leaves it in the list so you can reuse it or drop it later.

  3. 03You run git stash and your new, never-added file is still there. Why?

    Stash saves tracked files by default. Untracked files stay unless you use git stash push -u.

  4. 04A git stash pop hits a conflict. Is the stash lost?

    No. Git keeps the entry when the pop does not apply cleanly. Resolve the conflict, then drop the stash yourself with git stash drop.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark