Stashing work

Topic 3 of 5 in Rewriting history6 min readBeginnerFree course

Overview

You’re in the middle of something — files half-edited, nothing ready to commit — and you need a clean working directory right now: to switch branches, pull, or fix something urgent. git stash takes your uncommitted changes, puts them on a shelf, and gives you a clean working directory. When you’re ready, you take them back off the shelf.

git stash: put work aside for a moment. Half-finished edits in the working directory go onto the stash stack with git stash, where the newest entry is stash@{0}, then stash@{1} and stash@{2}; git stash pop brings them back. Meanwhile the directory is clean, so you can switch, pull or fix. Commands: git stash push -m “signup form”, git stash list, git stash pop, git stash -u to include untracked files, and git stash drop.

The basics

Terminal
git stash                 # shelve tracked changes (staged and unstaged)
git status                # nothing to commit, working tree clean
# …do something else…
git stash pop             # bring the changes back and remove them from the stash

git stash is short for git stash push. The stash is a stack: the newest entry is stash@{0}, the one before is stash@{1}, and so on.

Untracked files

By default, new files that were never added are left behind — they stay in your working directory. Usually you want them included:

Terminal
git stash -u              # --include-untracked
git stash -a              # --all: untracked AND ignored files (rarely needed)

Naming and listing

Terminal
git stash push -u -m "dashboard chart"
git stash list
Text
stash@{0}: On main: dashboard chart
stash@{1}: WIP on fix/login: 3c4d5e6 Fix login timeout

Without a message, Git uses “WIP on : ”, which gets confusing fast. Always name stashes you might keep for more than a minute.

Terminal
git stash show stash@{1}        # files changed
git stash show -p stash@{1}     # the full diff

Getting changes back

Terminal
git stash pop                   # apply stash@{0} and delete it
git stash apply                 # apply it but keep it in the list
git stash apply stash@{1}       # a specific one
git stash drop stash@{1}        # delete one without applying
git stash clear                 # delete them all (careful)

You can apply a stash on any branch, not just the one you stashed on — handy when you realise you started work on the wrong branch:

Terminal
git stash -u
git switch -c feature/correct-branch
git stash pop

Conflicts when applying

If the files changed since you stashed, applying can conflict. Resolve as usual — edit, remove markers, git add. With pop, Git keeps the stash when there’s a conflict (so nothing is lost); drop it yourself once you’ve resolved it.

Staged vs unstaged

Normally, applying a stash brings everything back as unstaged. To restore the staging area exactly as it was, use git stash pop --index. (Git 2.52+ also lets you make that the default with git config stash.index true.)

Stash part of your changes

Terminal
git stash push -m "just the CSS" -- styles/      # only these paths
git stash push -p                                # choose hunks interactively
git stash --keep-index                           # stash only what's NOT staged

--keep-index is useful to test exactly what you’re about to commit: stash everything else, run the tests, commit, then pop.

Turning a stash into a branch

If a stash turns out to be real work, give it a branch:

Terminal
git stash branch feature/chart

Git creates the branch at the commit you were on when you stashed, applies the stash there (so it can’t conflict) and drops it.

Stash or commit?

Stashes are convenient, but they’re easy to forget: they don’t show up in git log, they’re not pushed to GitHub, and a git stash clear removes them all. For anything longer than a quick interruption, a work-in-progress commit on a branch is safer:

Terminal
git switch -c wip/dashboard
git add -A && git commit -m "WIP: dashboard chart"
# later: carry on, then tidy up with git rebase -i or git reset --soft HEAD~1

Rule of thumb: stash for minutes, commit for hours. And check git stash list now and then — old stashes are usually forgotten work.

Exercise

Interrupted mid-feature

You’re halfway through a feature on main (edits to app.js and a new untracked file chart.js) when an urgent fix is needed. Stash everything, including the new file, with the message “dashboard chart”. Create fix/login, commit the fix, and merge it into main. Then bring your work back with git stash pop, check both files are back, and confirm the stash list is empty. Bonus: stash again and turn that stash into a new branch feature/chart.

Terminal
mkdir stash && cd stash && git init -b main
echo "app v1" > app.js && echo "login v1" > login.js
git add . && git commit -qm "Start app"

echo "app v1 + chart" > app.js          # unfinished
echo "drawChart()" > chart.js           # new, untracked

# 1. Stash everything with a message

# 2. Urgent fix on its own branch, merged into main

# 3. Bring the work back and check

# 4. Bonus: stash again and make a branch from it

Summary

  • git stash saves uncommitted changes and cleans the working directory so you can switch tasks.
  • git stash pop reapplies the latest stash and removes it; apply keeps it in the list.
  • Untracked files aren’t stashed unless you add -u (or -a for ignored files too).
  • Name stashes with -m, list them with git stash list, and inspect with git stash show -p.
  • For anything longer than a quick interruption, a commit on a branch is safer than a stash.