Stashing work
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.
The basics
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 stashgit 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:
git stash -u # --include-untracked
git stash -a # --all: untracked AND ignored files (rarely needed)Naming and listing
git stash push -u -m "dashboard chart"
git stash liststash@{0}: On main: dashboard chart
stash@{1}: WIP on fix/login: 3c4d5e6 Fix login timeoutWithout a message, Git uses “WIP on
git stash show stash@{1} # files changed
git stash show -p stash@{1} # the full diffGetting changes back
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:
git stash -u
git switch -c feature/correct-branch
git stash popConflicts 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
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:
git stash branch feature/chartGit 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:
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~1Rule of thumb: stash for minutes, commit for hours. And check
git stash listnow and then — old stashes are usually forgotten work.
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.
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 itmkdir 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
git stash push -u -m "dashboard chart"
git status --short # clean
git stash list # stash@{0}: On main: dashboard chart
# 2. Urgent fix on its own branch, merged into main
git switch -c fix/login
echo "login v2" > login.js && git commit -qam "Fix login timeout"
git switch main && git merge -q fix/login && git branch -d fix/login
# 3. Bring the work back and check
git stash pop
cat app.js chart.js # both back
git stash list # empty
# 4. Bonus: stash again and make a branch from it
git stash push -u -m "dashboard chart"
git stash branch feature/chart # new branch, stash applied and dropped
git status --short # M app.js / ?? chart.jsSummary
git stashsaves uncommitted changes and cleans the working directory so you can switch tasks.git stash popreapplies the latest stash and removes it;applykeeps it in the list.- Untracked files aren’t stashed unless you add
-u(or-afor ignored files too). - Name stashes with
-m, list them withgit stash list, and inspect withgit stash show -p. - For anything longer than a quick interruption, a commit on a branch is safer than a stash.