Skip to content

Git Stash: Save and Restore Work in Progress

Git stash temporarily shelves your uncommitted changes so you can work on something else.

The Basic Workflow

# You're working on a feature, but need to fix a bug urgently
git stash                    # save your work
git checkout main
# fix the bug, commit, push
git checkout feature
git stash pop                # restore your work

Stash Commands

git stash                    # stash tracked files
git stash push -m "WIP: refactoring auth"  # named stash
git stash -u                 # stash + untracked files
git stash -a                 # stash everything (including ignored)

git stash list               # show all stashes
git stash show               # show summary of latest stash
git stash show -p            # show full diff of latest stash

git stash pop                # apply + remove latest stash
git stash apply              # apply without removing
git stash apply stash@{2}    # apply a specific stash

git stash drop               # remove latest stash
git stash drop stash@{1}     # remove specific stash
git stash clear              # remove ALL stashes

Named Stashes

git stash push -m "refactor user model"
git stash push -m "fix login bug"
git stash push -m "add tests for api"

git stash list
stash@{0}: On main: add tests for api
stash@{1}: On main: fix login bug
stash@{2}: On main: refactor user model

git stash apply stash@{1}    # apply the "fix login bug" stash

Working with Untracked Files

By default, git stash only stashes tracked files:

# Stash including new (untracked) files
git stash -u

# Stash including everything
git stash --all

Partial Stash

Only stash specific files:

git stash push -m "only config changes" -- src/config.js

Or interactively:

git stash -p
# Git prompts you for each hunk — choose which to stash

Create a Branch from a Stash

If you stash on the wrong branch:

git stash branch new-feature stash@{0}

This creates a new branch at the commit where the stash was made, applies the stash, and drops it.

Recovering Dropped Stashes

If you accidentally drop a stash:

git stash drop stash@{1}  # oops

# Check git reflog for the stash commit hash
git fsck --unreachable | grep commit | cut -d' ' -f3 | xargs git log --oneline

# Apply the commit found
git cherry-pick <commit-hash>

Common Workflows

Interrupted Work

# Context: Working on feature-branch when urgent bug appears
git add -A
git stash save -u "feature X in progress"
git checkout main
git checkout -b hotfix/urgent-bug
# ... fix bug, commit, push ...
git checkout feature-branch
git stash pop

Testing Different Approaches

# Try approach A
git stash push -m "approach A"
# Try approach B
git stash push -m "approach B"
# Try approach C

# Compare approaches by applying in temp branches
git stash branch test-a stash@{2}
git stash branch test-b stash@{1}

Stash vs Commit

SituationStashCommit
Quick context switch
Work in progress, not ready
Saving progress long-term
Sharing with others
Testing multiple approaches
Need a clean working directory

Related: Learn how to undo commits and open pull requests.