Skip to main content

Git Stashing - Save Work in Progress

What is Git Stashing?

Git stashing allows you to temporarily save your uncommitted changes (both staged and unstaged) without creating a commit. This is useful when you need to switch branches or pull changes from remote without losing your work in progress.

The stash is a stack of saved changes that you can apply later.

When to Use Stashing

  • You're working on a feature but need to urgently switch to another branch
  • You need to pull remote changes that conflict with your work
  • You want to clean your working directory temporarily
  • You're experimenting and want to save the experiment without committing

Basic Stashing

Save Your Work

Save changes with git stash
git stash

This command:

  • Saves all staged and unstaged changes
  • Reverts your working directory to the last commit
  • Your changes are safely stored in the stash
Save with a descriptive message
git stash save "WIP: implementing user authentication"
# or the newer syntax
git stash push -m "WIP: implementing user authentication"

Descriptive messages make it easier to find the right stash later.

View All Stashes

List all stashes
git stash list

Output example:

stash@{0}: WIP: implementing user authentication
stash@{1}: WIP: fixing login bug
stash@{2}: WIP: updating database schema

Each stash has an identifier like stash@{0}. The most recent stash is stash@{0}.

See Stash Details

View what's in a stash
git stash show stash@{0}

This shows a summary of changed files.

See the actual changes in a stash
git stash show -p stash@{0}

The -p flag shows the diff of all changes in the stash.

Applying Stashed Changes

Apply and Keep Stash

Apply stash but keep it saved
git stash apply
# or apply a specific stash
git stash apply stash@{0}

This reapplies the changes but leaves the stash in the stash list.

Conflict Handling

If applying a stash causes conflicts, Git will mark conflicted files. You need to resolve conflicts manually before continuing.

Apply and Remove Stash

Apply stash and remove it
git stash pop
# or
git stash pop stash@{1}

This applies the stash and removes it from the list. This is the most common workflow.

Examples

Scenario 1: Quick branch switch

Example: Save work and switch branches
# You're on feature-branch with uncommitted changes
git stash
git checkout main
git checkout feature-branch
git stash pop

Scenario 2: Pull changes safely

Example: Stash before pulling
# Your changes conflict with remote
git stash
git pull origin main
git stash pop
# Resolve any conflicts that arise

Stash Management

Delete Stashes

Delete a specific stash
git stash drop stash@{0}
Delete all stashes
git stash clear
Permanent Deletion

Dropped stashes cannot be recovered. Use with caution!

Create Branch from Stash

Convert a stash into a new branch:

Create branch from stash
git stash branch new-feature

This creates a new branch and applies the stash to it. Useful if you realize your stashed work deserves its own branch.

Advanced Stashing

Stash Only Staged Changes

Stash only staged changes
git stash --keep-index
# or the newer syntax
git stash --staged

This stashes only changes you've staged with git add, leaving unstaged changes in your working directory.

Example: Stash unstaged changes only
# You have both staged and unstaged changes
git stash push -u -m "WIP: unstaged changes"
# This stashes both untracked and unstaged files

Stash Untracked Files

Include untracked files in stash
git stash -u
# or
git stash push -u

By default, git stash ignores untracked files. Use -u to include them.

Include ignored files too
git stash -a

The -a flag includes even ignored files.

Stash Specific Files

Stash only specific files
git stash push path/to/file1 path/to/file2 -m "WIP: specific files"

Practical Workflows

Workflow 1: Interrupting Work for Urgent Task

Urgent bug fix while working on feature
# You're implementing a new feature
vim feature.js
git status
# On branch feature-development
# Changes not staged for commit:
# modified: feature.js

# Critical bug reported - need to switch to main
git stash save "WIP: implementing new feature"

# Now working on bug fix
git checkout main
git checkout -b hotfix/critical-bug
vim bug.js
git add bug.js
git commit -m "Fix: critical production bug"
git checkout main
git merge hotfix/critical-bug

# Back to your feature work
git checkout feature-development
git stash pop

Workflow 2: Clean Workspace for Testing

Save changes, test clean state, restore
# You have experimental changes
git status

# Save and clean up
git stash save "WIP: experimental changes"

# Test with clean state
npm test

# All tests pass, restore your work
git stash pop

Workflow 3: Handling Merge Conflicts with Stash

Stash helps with merge conflicts
# Your branch with uncommitted work
git stash save "WIP: current work"

# Pull latest from main
git fetch origin
git rebase origin/main
# (might have conflicts)

# Rebase complete, restore your work
git stash pop
# (might have conflicts)

# Resolve any conflicts
git status
# Fix conflicts in files
git add resolved-files
git commit -m "Resolve merge conflicts"

Common Issues

Issue: Stash Pop with Conflicts

If git stash pop causes conflicts:

Resolve stash conflicts
# Stash pop created conflicts
# 1. Fix the conflicted files manually
vim conflicted-file.js

# 2. Stage the resolved files
git add conflicted-file.js

# 3. You still need to drop the stash manually
git stash drop stash@{0}

# Alternative: Use apply then drop
git stash apply stash@{0}
# (resolve conflicts)
git add .
git stash drop stash@{0}

Issue: Finding an Old Stash

Search stashes for specific content
# Search stash messages
git stash list | grep "search term"

# See changes in all stashes
git stash show -p | grep "specific code"

Issue: Accidentally Deleted Stash

Recover dropped stash
# See recent deletions
git reflog
# Look for 'stash@{..}' entries with 'drop'

# Recover the stash
git stash apply <hash-from-reflog>

Best Practices

  1. Use descriptive messages: git stash save "WIP: message" instead of unnamed stashes
  2. Don't stash long-term: Stash is temporary, commit or clean up stashes regularly
  3. Review stash before applying: Use git stash show -p before git stash pop
  4. Clear old stashes: Periodically run git stash list and clean up old stashes
  5. Create branches for important work: If you stash something multiple times, consider creating a branch instead
  6. Prefer switching branches early: Stash is great for quick switches, but plan your branches ahead

Stashing vs Committing

ScenarioStashCommit
Temporary WIP changes
Need to switch branches
Want to save for others to see
Don't have a final solution yet
Changes are tested and ready

Key Takeaways

  • git stash temporarily saves uncommitted changes
  • git stash list shows all saved stashes
  • git stash pop applies and removes a stash
  • git stash apply applies a stash without removing it
  • Use descriptive messages to track what each stash contains
  • Stash is temporary—commit completed work to the repository
  • Can create branches from stashes for longer-term work