GeekDas
••9 min read•
GeekdasGeekdas

Git Stash: Save Your Work Without Committing

Git Stash: Save Your Work Without Committing

If you've ever wondered "what is git stash", "what does git stash do", or looked for a simple git stash tutorial, this guide will explain everything in clear and practical terms. While working on a project, sometimes you need to switch branches or pull new changes, but your work is not ready to commit yet. This is where Git Stash becomes very useful.

Git stash lets you temporarily save your uncommitted changes, clean your working directory, and then bring those changes back later.

In this guide you will learn what stash in Git is, every command you need with examples, a realistic scenario you can follow along with, the mistakes that trip up beginners, and answers to frequently asked questions.

New to Git? Our guide to the 10 basic Git commands every beginner should know covers the foundations used in this tutorial.

What is Git Stash?

git stash takes your modified files (staged and unstaged) and stores them in a safe place. Your working directory becomes clean, just like the last commit.

You can think of stash as a clipboard for your code. You "cut" your unfinished work, do something else, and "paste" it back whenever you are ready.

A few important facts about how stash works:

  • It is a stack. The newest stash sits on top as stash@{0}, the one before it is stash@{1}, and so on.

  • It is local. Stashes live only in your own repository. They are not pushed to GitHub or GitLab, and teammates cannot see them.

  • It works across branches. You can stash on one branch and apply the changes on another.

  • By default it saves tracked files only. Modified and staged files are stashed, but brand-new untracked files are left behind unless you add a flag (covered below).

Why Not Just Commit?

You might wonder why you cannot simply make a quick commit. You can, but a half-finished commit with a message like "WIP" clutters your history and may break the build. Stash keeps your history clean.

Option

Best for

Downside

git stash

Quick, temporary context switch

Local only, easy to forget about

WIP commit

Work you want backed up on a branch

Messy history unless you clean it up later

New branch

Longer-running side work

More overhead for a small interruption

Basic Git Stash Commands

1. Stash your changes

This saves all your uncommitted changes to tracked files and resets your working directory.

git stash

Sample output:

Saved working directory and index state WIP on main: a1b2c3d Add homepage

git stash is shorthand for git stash push.

2. Stash with a message

Adding a message helps you remember why you stashed the changes. This is a habit worth building, because plain stashes all look identical in the list.

git stash push -m "work in progress on login page"

You may see older tutorials use git stash save "message". That form is deprecated, so use git stash push -m instead.

3. View all stashes

git stash list

Example output:

stash@{0}: On main: work in progress on login page
stash@{1}: On feature: navbar UI update

To see what a stash contains without applying it:

git stash show stash@{0}
git stash show -p stash@{0}

The first command shows a summary of changed files, and -p shows the full diff.

4. Apply the latest stash

This restores the changes but keeps the stash in the list.

git stash apply

Use apply when you want to reuse the same changes on more than one branch, or when you want a safety copy until you are sure everything works.

5. Apply and remove the stash

This restores the changes and deletes the stash after applying.

git stash pop

If applying the stash causes a merge conflict, Git keeps the stash in the list so you do not lose it. Resolve the conflict, then drop the stash manually once you are happy.

6. Apply a specific stash

git stash apply stash@{1}

You can use git stash pop stash@{1} the same way.

Tip for some shells: in PowerShell, and occasionally in other shells, the curly braces need quotes: git stash apply "stash@{1}".

7. Drop a stash

This deletes a specific stash.

git stash drop stash@{0}

8. Clear all stashes

This removes all stashed changes permanently.

git stash clear

Be careful here. Check git stash list first, because clearing is not easy to undo.

Command Cheat Sheet

Command

What it does

git stash

Stash tracked changes

git stash push -m "msg"

Stash with a descriptive message

git stash list

List all stashes

git stash show -p

Show the diff of the latest stash

git stash apply

Restore the latest stash and keep it

git stash pop

Restore the latest stash and delete it

git stash apply stash@{n}

Restore a specific stash

git stash drop stash@{n}

Delete one stash

git stash clear

Delete all stashes

Useful Stash Options

Include untracked files

New files that Git is not yet tracking are skipped by default. To include them:

git stash push -u -m "include new files"

-u is short for --include-untracked. Use -a (--all) to also include files ignored by .gitignore, which is rarely needed.

Stash only specific files

git stash push -m "only the navbar" src/navbar.css src/navbar.js

Choose changes interactively

git stash push -p

Git walks you through each change and asks whether to stash it, similar to git add -p.

Keep staged changes in place

git stash push --keep-index

This stashes your work but leaves staged changes in the working directory. It is handy for testing exactly what you are about to commit.

Restore staged state too

git stash apply --index

By default, changes that were staged come back as unstaged. Add --index to restore them as staged.

Turn a stash into a branch

git stash branch new-branch-name stash@{0}

This creates a branch from the commit where you stashed, applies the stash, and drops it if it applied cleanly. It is a great fix when the stash no longer applies cleanly to your current branch.

Example Usage Scenario

Imagine you are working on a feature and suddenly your manager asks you to fix a production bug.

Step 1: Save your current work

git stash push -m "halfway login form"

Your working directory is now clean.

Step 2: Switch branch and fix the bug

git checkout main

Fix the bug, commit it, and push it as you normally would. (On newer Git versions, git switch main does the same thing.)

Step 3: Come back and restore your work

git checkout feature/login
git stash pop

Your unfinished work is back exactly as it was.

Another Common Scenario: Pulling When You Have Local Changes

If you try to pull while you have uncommitted changes that overlap with incoming ones, Git may refuse. Stash makes this easy:

git stash
git pull
git stash pop

Git also has a shortcut that does the same thing in one command:

git pull --autostash

When Should You Use Git Stash?

  • When you need to quickly switch branches

  • When your work is not ready to commit

  • When pulling new changes causes conflicts

  • When you want to keep your commit history clean

  • When you want to test something on a clean working directory and then return to your changes

Common Problems and How to Fix Them

"I can't find my new file after stashing"

Plain git stash leaves untracked files in place, so a new file is not stashed (and stays visible). If you wanted it included, use git stash push -u.

"Stash pop caused a conflict"

Open the conflicted files, fix the marked sections, and stage them with git add. Because Git keeps the stash after a conflict, run git stash drop once you are satisfied the changes are correct.

"I dropped or cleared a stash by mistake"

You may still be able to recover it for a while. Run git fsck --no-reflog | grep "dangling commit" to list unreachable commits, then inspect candidates with git show <commit-id> and restore one with git stash apply <commit-id>. This only works until Git cleans up unreachable objects, so act quickly.

"I have too many stashes and don't know which is which"

Always use git stash push -m "message", and review the list with git stash list. Clean out old stashes regularly with git stash drop.

"Stash says 'No local changes to save'"

You have nothing modified in tracked files. Check git status to see whether your changes are in untracked files, in which case add -u.

Best Practices

  • Always add a message. Future you will thank present you.

  • Keep stashes short-lived. A stash is a temporary shelf, not long-term storage. For anything that will sit for days, use a branch or a commit.

  • Prefer apply when you are unsure. It keeps a safety copy until you drop it.

  • Check git status before and after. It confirms what was stashed and what was restored.

  • Remember stash is local. If your laptop dies, so do your stashes. Push important work to a branch.

Conclusion

Now you clearly understand:

  • What is git stash

  • What is stash in git

  • What does git stash do

  • How to use the git stash command

  • A practical git stash example

Git stash is a powerful feature that helps you manage unfinished work without committing it. By using stash correctly, you can work faster, switch contexts easily, and keep your repository clean.

If you're learning Git, mastering git stash is an essential skill. Try it on a practice repository: make a change, stash it, switch branches, and bring it back. A few minutes of hands-on practice will make it second nature.

Was this article helpful?

Comments

Leave a comment

Loading comments...

Frequently Asked Questions

What is git stash in simple words?
Git stash is a command that temporarily saves your uncommitted changes so you can work on something else without committing unfinished code.
What does git stash do exactly?
The git stash command stores your modified files in a hidden stack and restores your working directory to the last commit state.
When should I use git stash?
You should use git stash when switching branches, pulling updates, or handling urgent fixes while your current work is incomplete.
What is the difference between git stash apply and git stash pop?
"git stash apply" restores changes but keeps the stash saved. "git stash pop" restores changes and removes the stash from the list.
Does git stash save staged files?
Yes, git stash saves both staged and unstaged changes by default.
Is git stash permanent?
No. Git stash is temporary. If you run "git stash clear", all saved stashes are permanently deleted.
Can I stash only specific files?
Yes. You can stash specific files using: git stash push file-name

Related Articles