Master Git Stash Techniques: Save & Manage Changes

What is Git Stash?

Git stash is a powerful feature that allows you to temporarily save uncommitted changes without committing them to a branch. Think of it as a "shelf" where you can put your work aside, switch contexts, and come back to it later.

Simple Analogy: Imagine you're writing a document and need to work on something else urgently. Instead of saving a copy somewhere, you use a "stash drawer" to temporarily store your current work, clear your desk, and retrieve it exactly as you left it when ready to continue.

When to Use Git Stash

  • Context Switching: Quickly switch branches with uncommitted work
  • Emergency Fixes: Handle urgent bugs without losing current progress
  • Testing Changes: Test different approaches without committing
  • Clean Working Directory: Stash changes before pulling/merging
  • Work Organization: Temporarily store incomplete features

Pro Tip: Stash is perfect for short-term storage. For longer-term storage, consider creating a feature branch instead.

Basic Stash Commands

Stash Stack Visualization
0
WIP on feature/login: a1b2c3d Add login validation
Created: 2 hours ago
ACTIVE
1
WIP on feature/payment: c3d4e5f Update payment gateway
Created: 1 day ago

Essential Commands

Command Description Example
git stash Save uncommitted changes (both staged and unstaged) git stash
git stash save "message" Save with a descriptive message git stash save "login form WIP"
git stash list Show all stashes git stash list
git stash pop Apply and remove latest stash git stash pop
git stash apply Apply latest stash without removing git stash apply
git stash drop Delete latest stash git stash drop
git stash clear Delete all stashes git stash clear

Practical Workflow Examples

1
Emergency Context Switch
# Working on feature A
git status
# Shows modified: file1.js, file2.js

# Urgent bug fix needed
git stash
# Working directory now clean

# Switch to main for hotfix
git checkout main
git checkout -b hotfix/login-bug
2
Fix Bug and Return
# Fix the bug, commit it
# ... make changes ...
git add .
git commit -m "Fix login validation bug"

# Return to original feature
git checkout feature-a
git stash pop
# Changes restored, stash removed

Advanced Stash Techniques

1. Stashing Specific Changes

You don't have to stash everything. Use -p (patch) to selectively stash:

git stash -p

# Git will ask for each change:
Stash this hunk [y,n,q,a,d,e,?]?

# Options:
# y - stash this hunk
# n - don't stash this hunk
# q - quit (stash selected)
# a - stash this and all remaining
# d - don't stash this and all remaining

2. Stashing Only Unstaged Changes

Sometimes you want to keep staged changes:

# Stash only unstaged changes
git stash --keep-index

# Or use more intuitive command
git stash push -m "message"

# Verify staged changes remain
git status

3. Stashing with Message

Always use descriptive messages for better organization:

# Traditional way
git stash save "WIP: login form validation"

# Modern way (Git 2.13+)
git stash push -m "login form validation"

# View with messages
git stash list
# stash@{0}: On feature/login: login form validation
# stash@{1}: On main: experimental changes

4. Managing Multiple Stashes

Git maintains a stack of stashes. Learn to navigate them:

# List all stashes with details
git stash list --date=relative

# Apply specific stash by index
git stash apply stash@{2}

# Show what's in a stash
git stash show stash@{1} -p

# Drop specific stash
git stash drop stash@{1}

# Create branch from stash
git stash branch new-branch stash@{0}

5. Stashing Untracked Files

By default, stash ignores new files. Include them with -u:

# Stash everything including untracked files
git stash -u

# Stash everything including ignored files
git stash -a

# Modern equivalent
git stash push -u -m "includes new files"

Caution: Using -a (all) includes ignored files, which might include build artifacts or sensitive data. Use with care.

Use Cases & Best Practices

Branch Switching

Quickly switch between branches without committing half-done work. Perfect for multitasking developers.

git stash
git checkout other-branch
# Do work, then return
git checkout original-branch
git stash pop

Emergency Bug Fixes

Handle production issues immediately without losing your current feature work.

git stash save "feature/login WIP"
git checkout -b hotfix
# Fix bug, test, deploy
git checkout feature/login
git stash pop

Clean Pull/Merge

Stash changes before pulling updates to avoid merge conflicts in your working directory.

git stash
git pull origin main
git stash pop
# Resolve any conflicts that arise

Stash vs. Other Git Features

Feature When to Use Pros Cons
Git Stash Temporary storage (hours/days), quick context switches Fast, no commits needed, preserves work state Not versioned, can be lost, no remote backup
Temporary Commit Need to share or backup work Versioned, can be pushed, permanent record Pollutes history, requires cleanup
New Branch Long-term work, major feature development Proper version control, easy to share Overhead for small tasks
Git Worktree Parallel development on same repo No stash needed, separate directories Disk space, Git 2.5+ required

Stash Command Reference

Basic Operations

# Save all changes with message
git stash push -m "Descriptive message"

# List all stashes
git stash list

# Apply latest stash and remove it
git stash pop

# Apply latest stash (keep in stack)
git stash apply

Advanced Operations

# Show diff of specific stash
git stash show stash@{n} -p

# Create branch from stash
git stash branch branch-name stash@{n}

# Drop specific stash
git stash drop stash@{n}

# Clear all stashes
git stash clear

Partial Stashing

# Interactive stashing
git stash -p

# Stash only staged changes
git stash --staged

# Stash with include untracked
git stash -u

# Stash specific files
git stash push file1.js file2.js

Common Pitfalls & Solutions

1. Stash Conflicts on Pop/Apply

When applying stash causes conflicts:

# If conflicts occur during pop/apply
git status # See conflicted files

# Resolve conflicts manually
# Edit files, remove conflict markers

# After resolving:
git add resolved-file.js

# If using pop, stash is automatically dropped
# If using apply, manually drop stash:
git stash drop

2. Lost Stashes

Stashes can be recovered if you act quickly:

# Stashes are stored as commits
git fsck --unreachable | grep commit

# Find stash commits
git log --oneline --graph --all | grep -i stash

# Recover specific stash commit
git checkout abc1234 -- .

# Or use reflog
git reflog | grep stash

Important: Stashes are local only and can be garbage collected. Regularly convert important stashes to branches.

3. Stash Across Branches

Understanding how stashes work with branches:

# Stash created on feature/login
git stash save "login validation"

# Can apply to any branch
git checkout main
git stash apply

# But be careful! The stash contains
# the exact files from original branch
# which may not make sense elsewhere

Frequently Asked Questions

How long do stashes persist? Can they be lost?

Stashes are temporary and can be lost in several ways:

  • Git garbage collection: Runs automatically to clean up unreachable objects
  • Manual cleanup: git stash clear removes all stashes
  • Repository corruption: Rare but possible
  • Expiration: Stashes don't expire automatically but can be GC'd

Best practices to prevent loss:

  1. Convert important stashes to branches: git stash branch feature-name stash@{0}
  2. Regularly review and clean up old stashes
  3. Use descriptive messages to remember what each stash contains
  4. Consider committing instead of stashing for long-term work
What's the difference between git stash pop and git stash apply?

The key difference is whether the stash remains in the stack:

  • git stash pop: Applies the stash AND removes it from the stack
  • git stash apply: Applies the stash but leaves it in the stack

When to use each:

Use Case Command Why
One-time context switch git stash pop Clean up after yourself, no leftover stashes
Testing stash on different branches git stash apply Keep stash to try elsewhere
Applying same stash multiple times git stash apply Useful for testing or manual merging
Certain application git stash pop Prevents accidental re-application
Can I stash only staged changes or only unstaged changes?

Yes, Git provides several options for selective stashing:

# Stash only unstaged changes (keep staged)
git stash --keep-index

# Stash only staged changes (Git 2.35+)
git stash --staged

# Stash everything except specific files
git stash push -- ":(exclude)package.json"

# Interactive stashing (choose hunks)
git stash -p

# Stash specific files
git stash push file1.js file2.css

Real-world example: You've staged some changes for commit but want to test something. Stash the unstaged changes, test, then restore.

How do I share a stash with another developer?

Stashes are local only, but you can share them through several methods:

  1. Convert to branch and push:
    git stash branch shared-feature stash@{0}
    git push -u origin shared-feature
  2. Create patch file:
    git stash show -p stash@{0} > my-stash.patch
    # Send patch file to teammate
    git apply my-stash.patch
  3. Create temporary commit:
    # Stash, create temp branch, commit
    git stash
    git checkout -b temp-share
    git stash pop
    git add .
    git commit -m "WIP for sharing"
    git push origin temp-share

Note: For ongoing collaboration, consider using feature branches instead of stashes.

What's the best way to organize multiple stashes?

Managing multiple stashes effectively:

  • Use descriptive messages: git stash push -m "login: form validation WIP"
  • Regular cleanup: Review stashes weekly with git stash list
  • Convert to branches: For stashes older than a few days: git stash branch stash-name stash@{n}
  • Tag important stashes: Create tags for reference: git tag saved/login-validation stash@{0}^
  • Use aliases for common operations:
    # Add to .gitconfig
    [alias]
      sl = stash list
      ss = stash save
      sp = stash pop
      sd = stash drop
      sbr = stash branch

Golden rule: If a stash is important enough to keep for more than a week, it should probably be a branch.

Previous: Git Basics Next: Git Cherry-picking