WebTech1223411 logo WebTech1223411Web tech, read closely
Dev Tools

How to Use Git Stash Without Losing Uncommitted Work

You are halfway through a feature when a bug report lands, and you need a clean branch in the next two minutes without committing broken code.

Developer desk with a dark terminal window full of git commands on a monitor, a mechanical keyboard and a sticky note

You are halfway through a feature when a bug report lands, and you need a clean branch in the next two minutes without committing broken code. That is the moment git stash exists for, and it is also the moment people lose work with it, because they stash in a hurry and forget what they put away.

I use stash several times a week and have also rescued a few colleagues who thought their changes were gone. This guide covers the commands you actually need, the flags that prevent surprises, and how to recover a stash you dropped by mistake.

What git stash actually saves

When you run git stash, Git takes your uncommitted changes to tracked files, both staged and unstaged, and saves them as a special commit on a hidden stack. Then it resets your working directory to match HEAD. Your branch is clean, and the changes wait in the stack until you bring them back.

Two things are left out by default, and they cause most of the confusion:

  • Untracked files. A new file you have never added is not stashed. It stays in your working directory and follows you to the other branch.
  • Ignored files. Anything matched by .gitignore, such as build output or a local .env file, is left alone.

That first point catches people constantly. You stash, switch branch, and the new component file you created an hour ago is still sitting there, now mixed into the hotfix. Use git stash -u to include untracked files, or git stash -a to include ignored ones as well, though the second is rarely what you want.

The commands you will use every week

Stash has a lot of subcommands, but six cover nearly everything:

CommandWhat it does
git stash push -u -m "message"Saves tracked and untracked changes with a label
git stash listShows every stash, newest first, as stash@{0}, stash@{1} and so on
git stash show -p stash@{1}Shows the full diff of one stash without applying it
git stash apply stash@{1}Applies a stash and keeps it in the list
git stash popApplies the newest stash and removes it if there is no conflict
git stash drop stash@{1}Deletes one stash from the list

The message flag matters more than it looks. Without it, every entry reads "WIP on main" followed by a commit hash, and after a week you will have four of them with no idea which is which. A label like "half done search filter" takes three seconds and saves a lot of guessing.

Apply versus pop

Both commands bring changes back. The difference is what happens to the stash afterwards. Pop removes it from the list, apply keeps it.

If pop hits a merge conflict, Git keeps the stash and leaves conflict markers in your files, so nothing is lost. But if pop succeeds and you then realise you applied it to the wrong branch, the stash is gone from the list. You can still recover it, as shown later, but it is a nuisance.

My habit is to use apply, check that the result looks right and the tests pass, and only then run git stash drop. It is one extra command, and it means the stash is always there until I am sure.

Stashing only part of your changes

Sometimes you want to set aside one file or a few lines while keeping the rest. Two options help here.

Stash specific files

Pass paths after a double dash separator, for example git stash push -m "debug logging" followed by the separator and src/logger.js. Only that file is stashed. This is handy when you added temporary logging that you do not want in the commit.

Stash interactively

Run git stash push -p and Git walks through each change hunk, asking whether to stash it. Answer y or n for each one. It works like interactive staging and is ideal when one file contains both a real fix and some experiment you are not ready to keep.

There is also the keep-index option, which stashes everything but leaves your staged changes in place. I use it to test exactly what I am about to commit, with the unstaged noise temporarily removed.

Where I disagree with the usual advice

Plenty of tutorials present stash as the default way to switch tasks. Got interrupted? Stash it. I think that habit is where most lost work comes from. A stash is not on a branch, does not appear in your log, and is easy to forget for weeks. I have seen developers with 30 entries in their stash list and no memory of what any of them contain.

For anything you will not come back to within an hour or so, a work in progress commit on a branch is safer. Run git switch -c wip/search-filter, then git commit -am "wip", and switch away. The work now has a name, shows up in your branch list, and gets pushed if you push the branch. When you return, git reset HEAD~1 turns it back into uncommitted changes. Stash is great for quick interruptions of a few minutes. It is a poor filing cabinet.

Recovering a dropped stash

If you dropped or popped a stash and need it back, there is usually a way. A stash is a commit, and Git does not delete unreferenced commits right away. They stay in the object database until garbage collection removes them, which by default is at least two weeks.

  1. If you just ran drop, Git printed a line like "Dropped stash@{0} (abc1234...)". That hash is your stash. Run git stash apply abc1234 to bring it back.
  2. If the terminal output is gone, run git fsck with its no-reflog option and look for lines starting with "dangling commit".
  3. Inspect candidates with git show followed by the hash. Stash commits have a message starting with "WIP on" or "On" followed by your branch name, which makes them easy to spot.
  4. Once you find it, apply it with git stash apply and the hash, or create a branch from it with git branch recovered-work and the hash.

This is also a good reason to label your stashes. Searching dangling commits for "half done search filter" is much faster than reading ten anonymous diffs. When you are tracking down which change actually broke something, git bisect is the companion tool worth learning next.

A few habits that keep stash safe

  • Always add -m with a short message.
  • Add -u whenever you have created new files.
  • Prefer apply, check, then drop over pop.
  • Run git stash list at the end of each week and clear out anything you no longer need.
  • Move anything older than a day onto a proper branch.

Clean stashes also make tooling quieter. If your formatter runs on commit, as described in setting up Prettier and ESLint so they stop fighting, a stray stashed change will not trip it up later.

Frequently asked questions

Does git stash save untracked files?

Not by default. A plain git stash saves only changes to tracked files. Add -u to include untracked files, or -a to include both untracked and ignored files.

What is the difference between git stash pop and apply?

Both restore the stashed changes to your working directory. Pop also removes the stash from the list if the apply succeeds, while apply keeps it. Using apply and dropping manually is safer when you are unsure.

How do I recover a dropped git stash?

Use the hash Git printed when you dropped it, and run git stash apply with that hash. If you no longer have it, git fsck with the no-reflog option lists dangling commits, and stash commits are easy to recognise by their "WIP on" message.

MC
Maren Castellanos

Maren worked in cross-browser QA before editing, and knows the DevTools Network tab better than most people know their inbox. She writes about how browsers load and render pages and about the tools that show what is really happening.

More posts by Maren

More in Dev Tools

01doorganico 084goad 3hot8 4465456 after5internprogram ahandfulofblessings ahmadpc ahomcybersecurity allodialresources annodex antiquesandperiodcollectibles aotc asianworldkashmir beedlectrics bloggertemplatefree bluefeatheragency bodyweightsolution campaign08blog carfixadvisor catstreetcollective cbstvsports cnfanspurchasing committeeof500yearsofdignityandresistance covertopsdjs cs-kaiyuangame cuestacomunicaciontotal cxoutlet dancebetgame deskgearhub destinyvillastay detectmagazine di33 diamondsteelbuildings donmo dyanadropship emarsysfinance emimattl escapechan eskitarih evautoplay everythingweddings-orlando farmzone feedviewerapp foxstudiosscratch gamevui123 gleasongrade greetingsday hacklabasu haskell iw-kaiyun jagoanproperti jilidskripsi joltbusinessfinance juegodepokergratis juegospucca kaeru-festival latestmehndidesigns lestanzediottaviano litemagazines localcommunityhero lustrsazan mariashebets menswheels newwebstechs1 nikkit ootheday oxigenvpn plurfitness prefetial probudgetcarpetcleaning psychicmrsmartin pyaten qvantor rejoignezlogis rexzbot riselayouts ruchin rwxqsgd5 sakaltime shibiaowang shop-bot skrtips spotlesshaven strcoding tetapatrium topselller uneplanteartificielle vipertechcr wo-kygaming yamaha-mekar yeknava yoybuysheets zenunwa yamaha-mekar jilidskripsi ankertoto ankertoto ankertoto