What git stash does and when to use it

Git stash temporarily saves changes you've made to tracked files without creating a commit. When you run git stash, Git takes your modified files, saves them to a separate storage area, and reverts your working directory to the last commit. You can then switch branches, pull updates, or work on something else — and come back later to restore those changes.

The most common reason to stash is switching branches when you have uncommitted work. If you try to switch branches with unsaved changes, Git will block you to prevent losing work. Stashing lets you set your current work aside safely, switch branches, and return to it later. Other reasons include pulling updates from a remote repository, cleaning up your working directory to see what's actually changed, or temporarily reverting to test something without committing.

Stash is not a permanent storage — it's a temporary holding area. Changes in stash will eventually be lost if you don't restore them, so treat stash as a short-term tool, not a backup.

Key Takeaways

  • Run git stash to save your current changes without committing, then your working directory returns to the last commit.
  • Use git stash list to see all your stashed changes, which are numbered starting from stash@{0}.
  • Restore stashed changes with git stash pop to apply them and remove the stash, or git stash apply to keep the stash in storage.
  • Name your stashes with a message using git stash save "description" so you remember what each one contains.
  • Delete a stash permanently with git stash drop stash@{number}, or clear all stashes with git stash clear.

Saving changes with git stash

To stash your current changes, open your terminal in your repository and run:

git stash

Git will save all modifications to tracked files and return your working directory to the state of the last commit. Any untracked files (files Git has never seen before) are left alone — only files Git is already tracking get stashed. If you want to include untracked files, use git stash -u or git stash --include-untracked.

By default, Git gives your stash a generic name like "WIP on main" (WIP stands for "work in progress"). To make it easier to remember what you stashed, add a message:

git stash save "message describing your changes"

For example: git stash save "work on login form validation". This message appears when you list your stashes later, so you can quickly find the one you need.

Viewing and listing your stashes

To see all the changes you've stashed, run:

git stash list

This shows a numbered list of all your stashes. The most recent stash is stash@{0}, the one before that is stash@{1}, and so on. Each line includes the stash number, the branch it was created on, and the message you gave it (or the default message if you didn't add one).

To see the actual changes inside a specific stash without restoring it, run:

git stash show stash@{0}

This shows which files changed and how many lines were added or removed. To see the full diff of what's in the stash, add the -p flag:

git stash show -p stash@{0}

Restoring stashed changes

When you're ready to bring your stashed changes back, you have two options. git stash pop restores the most recent stash and removes it from storage:

git stash pop

If you want to restore a specific stash (not the most recent one), include its number:

git stash pop stash@{2}

Alternatively, git stash apply restores a stash but keeps it in storage, so you can apply it multiple times if needed:

git stash apply stash@{0}

Use apply when you want to test the changes on a different branch or keep a backup. Use pop when you're confident you want to restore and discard the stash.

Handling conflicts when restoring

If the code has changed since you stashed your work, Git may report a conflict when you try to restore. This happens when the same lines were modified in both the stash and the commits that came after. Git will mark the conflicting sections in your files with conflict markers — lines starting with <<<<<<<, =======, and >>>>>>>.

Open the conflicted files and decide which version to keep. Delete the conflict markers and the code you don't want, then save the file. After resolving all conflicts, run git add on the resolved files and continue your work. If the conflict is too messy, you can run git stash drop to discard the stash and start over.

Deleting stashes you no longer need

Stashes take up space in your repository, so it's good practice to clean them up. To delete a single stash, run:

git stash drop stash@{0}

Replace 0 with the number of the stash you want to remove. To delete all stashes at once, run:

git stash clear

Be careful with clear — it removes every stash without asking for confirmation. If you're unsure whether you need a stash, use git stash show -p to review it first, or use drop to remove them one at a time.

Converting a stash into a commit

If you decide your stashed work is worth keeping permanently, you can turn it into a real commit. First, restore the stash:

git stash pop

Then stage and commit the changes as you normally would:

git add .

git commit -m "your commit message"

This gives your work a permanent place in your repository history. Stashing is useful for temporary work, but commits are the proper way to save changes long-term.

Frequently Asked Questions

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

Pop restores your stashed changes and deletes the stash from storage. Apply restores the changes but keeps the stash, so you can apply it again later. Use pop when you're done with the stash, and apply when you want to test the changes on multiple branches or keep a backup.

Can I stash changes on one branch and restore them on a different branch?

Yes. Stashes are not tied to a specific branch — they're stored in your repository. You can stash on the main branch, switch to a feature branch, and restore the stash there. This is useful when you accidentally start work on the wrong branch.

What happens to untracked files when I stash?

By default, untracked files (files Git has never seen) are not stashed — only tracked files are saved. To include untracked files, use git stash -u or git stash --include-untracked. Ignored files (listed in .gitignore) are never stashed.

Can I stash only certain files instead of all changes?

Yes, use git stash push -m "message" path/to/file to stash specific files. For example, git stash push -m "form changes" src/form.js stashes only that file. You can list multiple file paths to stash more than one.

What if I accidentally deleted a stash I needed?

If you deleted a stash recently, you may be able to recover it using git reflog, which shows a history of all Git operations. However, recovery is not may provide, especially if you ran git stash clear. This is why it's safer to convert important work into a commit rather than leaving it in stash long-term.