Tech News, Tech & Coding · Free Online Tools

IT Question Answer
Back to Git
Git Stash: Save Changes Temporarily

Git Stash: Save Changes Temporarily

GitBy Muhammad Fareed
gitstashsavechangestemporarily

Short answer: git stash takes your uncommitted changes, saves them on a stack and restores a clean working directory. You can then switch branches or pull updates, and later bring the changes back with git stash pop.

When to use it

You are halfway through a change when an urgent bug needs fixing on another branch. Your work is not ready to commit, and Git will not let you switch branches if it would overwrite your changes. Stash solves this in two commands.

The basic workflow

git stash                 # save changes, clean the working directory
git switch hotfix/login   # do the urgent work
# ... fix, commit, push ...
git switch feature/search
git stash pop             # restore your changes and drop the stash

By default git stash saves modified tracked files and staged changes. It does not include new untracked files or ignored files.

Stash with a message

git stash push -m "WIP: search filters"

Names make a long list readable.

Include untracked and ignored files

git stash push -u        # include untracked files
git stash push -a        # include untracked AND ignored files

Stash only some files

git stash push -m "only styles" -- src/styles/main.css
git stash push -p        # choose hunks interactively

Managing the stash list

git stash list           # stash@{0}: On feature/search: WIP: search filters
git stash show           # summary of the latest stash
git stash show -p stash@{1}   # full diff of a specific stash
git stash apply          # restore but KEEP the stash
git stash pop            # restore and REMOVE the stash
git stash drop stash@{1} # delete one stash
git stash clear          # delete all stashes (cannot be undone easily)

apply vs pop

CommandRestores changesKeeps the stash
git stash applyYesYes
git stash popYesNo (removed if it applies cleanly)

Use apply when you want to apply the same changes to more than one branch.

Create a branch from a stash

git stash branch fix/search-bug stash@{0}

This creates a new branch at the commit where you stashed, applies the stash and drops it. It is the cleanest way out when applying fails because the code has moved on.

Handling conflicts

If the stash does not apply cleanly, Git reports conflicts and keeps the stash so you do not lose work. Resolve the files as in resolving merge conflicts, then git add them and drop the stash manually with git stash drop.

Recovering a dropped stash

git fsck --no-reflog | grep "dangling commit"
git show <commit-hash>
git stash apply <commit-hash>

This works only until Git cleans up unreachable objects, so act soon.

Tips and cautions

  • Stashes are local; they are not pushed to the remote.
  • They are easy to forget. If work matters, commit it on a branch instead; a WIP commit can be amended later.
  • Keep the list short and clear old stashes.
  • Combine with a good branching strategy to avoid needing stash too often.

More commands are in the Git commands cheat sheet.

FAQs

Where does Git store stashes?

In a special reference in your local repository (refs/stash) as commits. They are not part of any branch.

Does git stash save untracked files?

Not by default. Add -u to include them.

Can I stash on one branch and apply on another?

Yes. A stash is not tied to a branch, but it may conflict if the target branch has changed the same lines.

Is stash better than a temporary commit?

For quick switches, stash is fine. For anything you want to keep safely, a commit on a branch is more reliable.