G
GuideDevOps
Lesson 8 of 18

Git Stash

Part of the Git & Version Control tutorial series.

What is Stash?

Stash saves uncommitted changes temporarily, leaving your working directory clean.

Basic Stash

# Save changes
git stash
 
# Shows: Saved working directory and index state WIP on main: abc1234 Last commit
 
# Working directory is now clean
git status
# nothing to commit, working tree clean

Using Stashed Changes

Apply Stash

# Apply most recent stash (doesn't delete it)
git stash apply
 
# Apply specific stash
git stash apply stash@{2}
 
# Apply and remove in one command
git stash pop

List Stashes

git stash list
# stash@{0}: WIP on main: abc1234 My commit
# stash@{1}: WIP on feature: def5678 Another commit
# stash@{2}: On feature: ghi9012 Yet another

View Stash Contents

# See what's in most recent stash
git stash show
 
# See detailed diff
git stash show -p
git stash show stash@{1} -p

Managing Stashes

Delete Stash

# Delete specific stash
git stash drop stash@{1}
 
# Delete all stashes
git stash clear

Named Stashes

# Stash with descriptive name
git stash save "WIP on search feature"
# or newer syntax:
git stash push -m "WIP on search feature"
 
# List shows names
git stash list
# stash@{0}: WIP on search feature

Practical Scenarios

Switch Branches with Uncommitted Work

You need to switch branches but have uncommitted changes:

git checkout feature/search
# error: Your local changes to 'index.ts' would be overwritten
 
# Stash the changes
git stash
 
# Now you can switch
git checkout feature/search
 
# Later, switch back and restore
git checkout main
git stash pop

Clean Up Stashes from Specific Branch

# Stash on main
git stash
 
# Create branch from stash
git stash branch feature/fixes
# Creates new branch and applies stash
 
# If stash was already applied elsewhere:
git stash drop stash@{0}

Apply Stash to Different Branch

# Stashed changes on main
git stash
 
# Switch to feature branch
git checkout feature/auth
 
# Apply stash there
git stash apply

Advanced Stash Options

Stash Specific Files

# Stash only tracked files (not new ones)
git stash
 
# Stash new untracked files too
git stash -u  # --include-untracked
 
# Stash only staged changes
git stash --staged

Keep Index

# Stash but keep staged changes
git stash --keep-index
 
# Useful to preserve staging area

Stash with Partial Changes

# Interactive stash (like git add -p)
git stash -p
 
# Choose hunks to stash interactively
 
# Remaining changes stay in working directory

Creating Commits from Stash

# Convert stash to proper commit
git stash apply stash@{0}
git add files
git commit -m "Proper commit message"
 
# Clean up
git stash drop stash@{0}

Workflow Examples

Feature Branch Interrupted

# Working on feature
git switch -c feature/search
# Make changes
git add search.ts
 
# Urgent bugfix needed
git stash                    # Save progress
git switch main
git switch -c hotfix/urgent
# Fix and commit
git switch main
git merge hotfix/urgent
 
# Resume feature work
git switch feature/search
git stash pop

Multiple Stashes

git stash push -m "filters UI"
git stash push -m "search algorithm"
 
git stash list
# stash@{0}: On main: search algorithm
# stash@{1}: On main: filters UI
 
# Apply different ones as needed
git stash apply stash@{1}

Tips

✓ Use stash to temporarily save WIP
✓ Name stashes to remember what's in them
✓ Use git stash list to avoid losing work
✓ Apply changes on different branches if needed
✓ Regular review and cleanup of old stashes
✓ Consider committing instead of stashing long-term