What git stash does and when to use it

Git stash temporarily saves changes you have made to files but have not committed yet, and clears them from your working directory. When you run git stash, Git takes all your uncommitted modifications — both staged and unstaged — and stores them in a separate location. Your working directory returns to the state of your last commit, as if you had not made those changes at all.

You use stash when you need to switch branches or pull new code but do not want to commit your current work. The most common scenario: you are in the middle of editing a feature, your team pushes an urgent fix to the main branch, and you need to check it out without losing your progress. Stash lets you set your work aside, switch branches, and come back to it later.

Stash is not a permanent storage system. It is a temporary holding area. If you stash changes and then delete the stash, those changes are gone. For work you want to keep long-term, you should commit it to a branch instead.

Key Takeaways

  • git stash saves your uncommitted changes and clears your working directory without creating a commit.
  • git stash pop restores your most recent stash and removes it from the stash list.
  • git stash list shows all your saved stashes, labeled with a number and the branch you were on.
  • You can stash multiple times, and each stash is stored separately — use git stash pop stash@{0} to restore a specific one.
  • Stash is temporary storage only; if you do not restore it before deleting it, the changes are lost.

The basic stash command

To stash your current changes, type:

git stash

Git will save all modified files in your working directory and return you to a clean state. You will see output like:

Saved working directory and index state WIP on main: a1b2c3d Your last commit message

The WIP stands for "work in progress." Git has recorded which branch you were on and what your last commit was, so it can help you restore the right context later.

If you have untracked files — files Git has never seen before — git stash will not touch them by default. If you want to stash untracked files too, use:

git stash -u

The -u flag stands for "untracked." This is useful when you want a completely clean working directory before switching branches.

Restoring your stashed changes

To bring your stashed changes back into your working directory, use:

git stash pop

git stash pop restores the most recent stash and removes it from your stash list. If the restore succeeds without conflicts, the stash is deleted. If there are conflicts — because the code has changed since you stashed — Git will tell you which files have conflicts and leave the stash in place so you do not lose it.

If you want to restore a stash without deleting it, use:

git stash apply

git stash apply does the same thing as pop, but leaves the stash in your list. This is useful if you want to restore the same changes to multiple branches, or if you want to keep a backup before you try to apply it.

Working with multiple stashes

Every time you run git stash, a new stash is created and added to a stack. You can see all your stashes by running:

git stash list

The output will look like:

stash@{0}: WIP on main: a1b2c3d Your last commit messagestash@{1}: WIP on feature-branch: x9y8z7w An older commit messagestash@{2}: WIP on main: m5n4o3p Another old commit message

The number in braces is the stash index. stash@{0} is always the most recent stash. When you run git stash pop with no arguments, it pops stash@{0}.

To restore a specific stash, include its index:

git stash pop stash@{1}

This restores the stash at index 1 and removes it from the list. You can also use git stash apply stash@{1} to restore it without deleting it.

Viewing what is in a stash

Before you restore a stash, you might want to see what changes it contains. Use:

git stash show stash@{0}

This shows a summary of which files changed and how many lines were added or removed. To see the actual diff — the line-by-line changes — use:

git stash show -p stash@{0}

The -p flag stands for "patch" and shows the full diff. This is useful when you have multiple stashes and need to remember which one contains the work you want to restore.

Deleting stashes

Stashes do not disappear on their own. If you decide you do not need a stash, you can delete it:

git stash drop stash@{0}

This removes the stash at index 0 from your stash list permanently. If you want to delete all stashes at once, use:

git stash clear

Be careful with git stash clear — it deletes every stash you have saved, and there is no way to undo it. Only use this if you are certain you do not need any of your stashed work.

Common stash workflows

The most common use case: you are working on a feature branch, someone asks you to fix a bug on main, and you do not want to commit your incomplete work.

Run git stash to save your changes. Then run git checkout main to switch branches. Your working directory is now clean and you can pull the latest code and make your fix. When you are done, switch back to your feature branch with git checkout feature-branch and run git stash pop to restore your work.

Another scenario: you have made changes but realize you are on the wrong branch. Stash your changes, check out the correct branch, and pop the stash there. Git will try to apply your changes to the new branch. If the files have the same content, it works seamlessly. If the branches have diverged significantly, you may see conflicts that you will need to resolve manually.

Frequently Asked Questions

What happens if I stash changes and then switch branches?

Your stashed changes stay in the stash list and are not lost. You can switch to any branch and restore them there using git stash pop or git stash apply. Git does not tie a stash to a specific branch — it is stored globally in your repository.

Can I stash only some of my changes, not all of them?

Yes. Use git stash push -p to enter interactive mode. Git will show you each change and ask whether you want to stash it. Answer y for yes or n for no. This lets you stash only the changes you want to set aside.

What if I pop a stash and there are conflicts?

Git will mark the conflicting sections in your files and leave the stash in place. You will need to manually edit the files to resolve the conflicts, then stage and commit your changes. Once you have resolved everything, you can safely delete the stash with git stash drop.

Is stash the same as a commit?

No. A commit is permanent and becomes part of your branch history. A stash is temporary and exists only until you restore or delete it. Stashes do not appear in git log and are not shared when you push to a remote repository.

Can I name my stashes to make them easier to remember?

Yes. Use git stash save "your description here" to give a stash a custom message. This message will appear in git stash list so you can identify which stash contains which work. Some versions of Git use git stash push -m "your description" instead.