Git Stash: Shelve Messy Work, Fix the Bug, Get Everything Back

Terminal showing git stash push saving work-in-progress changes, followed by git status reporting a clean working tree

Git stash is the command that shelves your uncommitted changes in one move, hands you a clean working tree, and lets you bring every change back later. An urgent bug just landed. Your feature is half-built. Your working tree is a mess. You don't have to commit garbage to escape.

This guide walks through the full workflow in order. You'll save work with a readable label, catch the untracked files a plain stash leaves behind, and find the right entry. Then you'll inspect it, restore it with pop or apply, verify it came back intact, and recover when a restore goes sideways.

TL;DR
  • git stash push -m "label" saves your changes and leaves a clean working tree.
  • Add -u, or brand-new untracked files stay behind and follow you to the next branch.
  • git stash list shows every entry, and stash@{0} is always the newest.
  • git stash show -p lets you read the full diff before you restore anything.
  • pop restores and deletes the entry. apply restores and keeps it as a backup.
  1. 0:00Intro
  2. 0:26Shelve, Switch, Restore Fearlessly
  3. 0:58Save Work with stash push
  4. 1:39Include Untracked Files with -u
  5. 2:22List Every Saved Stash
  6. 3:00Inspect a Stash with show -p
  7. 3:41Restore Work with stash pop
  8. 4:16Choose pop or apply
  9. 4:51Verify Changes Reappear Intact
  10. 5:29Fix Stash Errors Fast
  11. 6:03Your Stash Workflow in Five
  12. 6:26Stash fast, switch fearlessly, restore exactly

Check You're Ready to Stash

Here's the goal: shelve uncommitted changes, jump to another task, then restore it all untouched. That's the exact moment stash was built for. You're mid-feature, a bug needs fixing now, and you're asking how to switch without a junk commit.

The setup is light. You need Git installed and a repository with tracked changes in it. You should be comfortable moving between branches. And you need to know git status, because you'll read it constantly. It's how you see what's dirty and what's clean at every step of this workflow.

  • Git is installed and working
  • You're inside a repo with tracked changes
  • You can switch between branches
  • You can read git status output

Save Work with git stash push

Step one: get the mess off your desk. Run git stash push with -m and a label, like wip: login form. Git confirms it saved your working directory and index state. Run git status and you'll see nothing to commit, working tree clean.

Always add a message. Without -m, Git writes a vague auto-message based on your last commit. That's fine with one stash. With three piled up, guessing which is which gets dangerous fast. The -m flag turns stash@{0} into something you'll recognise later.

Once the tree is clean, you're free. Check out main or a hotfix branch right now. No junk commit, no lost work, no hesitation.

  • Use -m with a label that names the work
  • Confirm the 'Saved working directory and index state' message
  • Run git status and look for 'working tree clean'
git stash push -m "wip: login form"
git status

Include Untracked Files with -u

This is the trap that bites almost everyone. A plain stash ignores untracked files by default. Brand-new files you haven't added yet stay right where they are.

Look at the example. git status --short shows src/Login.js as modified (M) and src/Login.test.js with question marks, meaning untracked. Run git stash push -u -m "wip: login + tests". Git saves it, and the short status comes back empty. Both files are on the shelf.

Why does this matter? A forgotten new file stays in your tree and tags along to the hotfix branch. Suddenly your bug fix ships with a half-written test. Plain stash saves tracked changes but can leave the tree looking dirty. Stash push -u saves both, and the tree is truly clean.

  • Run git status --short and look for ?? entries
  • If any appear, stash with -u
  • Re-run git status --short and confirm it's empty
git status --short
git stash push -u -m "wip: login + tests"
git status --short

List Every Stash You've Saved

Bug fixed? Time to find your work. Stashes stack up, and git stash list shows every entry, newest first. In the example, stash@{0} is on feature/login with the label wip: login + tests. stash@{1} is on main with wip: navbar tweak. Each line tells you the branch and your label.

Remember that the newest is always zero. Every new stash pushes the others down one index, so stash@{1} today might be stash@{2} tomorrow. Don't memorise numbers. Read labels. This is where your -m messages pay off: you see login + tests and know instantly it's the one you want.

  • stash@{0} is always the most recent entry
  • Check the branch name on each line
  • Match the entry by its label, not its index
git stash list

Inspect a Stash Before Restoring It

Found the entry? Don't restore it blind. git stash show -p prints the full diff stored in a stash. The -p means patch: line-by-line changes. Running git stash show -p stash@{0} gives a normal diff of src/Login.js, a hunk header, then the added lines, including the email and error state you were writing.

Pick by index. Leave the index off and Git shows the newest. Name stash@{1} to inspect the older navbar work instead. Ten seconds of reading saves you from dumping the wrong changes into a branch you just cleaned.

There's zero risk here. show only reads. It changes nothing in your tree, so inspect as often as you like before committing to a restore.

  • Add -p to see the full diff, not just a summary
  • Name an index to inspect an older stash
  • Confirm the changes match the work you expect
git stash show -p stash@{0}

Restore Work with git stash pop

You've confirmed it. Now bring it back. git stash pop reapplies the newest stash, then removes it from the list. Switch back with git switch feature/login, then run git stash pop. src/Login.js shows as modified again, src/Login.test.js is back as untracked, and Git reports it dropped stash@{0}.

Switch first. This order matters. Pop applies to whatever branch you're on, so popping on main drops your login work in the wrong place.

After a clean pop, the entry is gone and your stash list shrinks by one. That's a feature, not a loss. No stale stashes piling up for months.

  • Switch to the branch where the work belongs
  • Run git stash pop
  • Look for the 'Dropped refs/stash@{0}' line
git switch feature/login
git stash pop

Choose Between pop and apply

Pop isn't your only option. Sometimes you want the changes back and the safety net kept. git stash apply restores the same changes into your tree but keeps the stash entry in the list. Same restore, different cleanup.

Pop is best for normal everyday restores. Apply fits when you want to reuse the same work on several branches, or when a restore feels risky. Your stash stays put until you've checked everything works. With apply, cleanup is your job: run git stash drop with the index, such as git stash drop stash@{0}. You get safety now and a clean list later.

  • pop: restores changes, drops the entry after a clean apply
  • apply: restores changes, keeps the entry
  • Use apply to reuse work on several branches
  • Drop the entry yourself once you trust the result

Verify Your Changes Came Back Intact

Restored? Prove it. Two commands, five seconds. Run git status --short. src/Login.js is modified and the test file is untracked, just like before you stashed. Then run git diff --stat: src/Login.js, two insertions. That matches the diff you inspected earlier.

Check one is same files back. Status should list every file you stashed. If the untracked test file is missing, you stashed without -u, and it's still sitting in your tree. Check two is same lines back. Compare git diff with your earlier show -p output. When both match, you know nothing was lost.

  • git status --short lists every stashed file
  • git diff --stat matches what show -p displayed
  • A missing new file means the stash lacked -u
git status --short
git diff --stat

Fix Common Git Stash Errors

Verify didn't go clean? Don't panic. Good news first: a failed pop never loses your stash. If pop hits a conflict, Git keeps the entry. Resolve the conflicted file, git add it, then run git stash drop yourself, because the stash was kept.

If Git says local changes would be overwritten, commit or stash your current changes first, then try again. If a new file is missing, it never left your tree. Use -u next time.

When a restore is too messy, run git stash branch. It creates a new branch at the commit you stashed from, applies your work there, and drops the stash. Your messiest restore becomes a clean one.

  • CONFLICT after pop: resolve, git add, then git stash drop
  • 'Local changes would be overwritten': commit or stash first
  • Missing new file: stash again with -u
  • Too tangled: git stash branch to restore on a fresh branch

The bottom line

  • Stash with git stash push -m and a clear label, every time.
  • Add -u whenever git status shows untracked files.
  • Find entries with git stash list and read labels, not index numbers.
  • Inspect with git stash show -p before restoring anything.
  • Pop to restore and clean up. Apply to restore and keep a backup.
  • On conflicts, resolve, git add, then drop the stash yourself.

Quick-fire questions

Does git stash save untracked files?

Not by default. A plain stash saves tracked changes and leaves brand-new untracked files in your working tree. Add -u, as in git stash push -u, to include them.

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

Both restore the stashed changes into your working tree. Pop drops the stash entry after a clean apply, while apply keeps it in the list. Use apply when you want a backup or need the same work on several branches, then remove it with git stash drop.

Do I lose my stash if git stash pop has a conflict?

No. When pop hits a conflict, Git keeps the stash entry. Resolve the conflict, git add the file, then run git stash drop to remove the entry yourself.

How do I see what's inside a stash before restoring it?

Run git stash show -p, optionally followed by an index like stash@{1}. It prints the full line-by-line diff and changes nothing in your working tree, so it's safe to run as often as you like.

Why should I add a message to git stash?

Without -m, Git uses a vague auto-message based on your last commit. Once several stashes pile up, those messages are hard to tell apart, and indexes shift with every new stash. A label like wip: login form makes the right entry obvious in git stash list.

What does git stash branch do?

It creates a new branch at the commit you originally stashed from, applies your stashed work there, and drops the stash. It's the clean way out when restoring onto your current branch gets messy.

Watch the full video on YouTube →

Souy Soeng

Souy Soeng

Hi there 👋, I’m Soeng Souy (StarCode Kh)
-------------------------------------------
🌱 I’m currently creating a sample Laravel and React Vue Livewire
👯 I’m looking to collaborate on open-source PHP & JavaScript projects
💬 Ask me about Laravel, MySQL, or Flutter
⚡ Fun fact: I love turning ☕️ into code!

Post a Comment

CAN FEEDBACK
Ad