Git Stash Workflow - Complete Guide
Published: September 25, 2024 | Reading time: 17 minutes
Git Stash Overview
Git stash temporarily saves uncommitted changes:
Stash Benefits
# Git Stash Benefits
- Temporary change storage
- Clean working directory
- Quick context switching
- Change preservation
- Flexible workflow
- Easy change recovery
- Branch switching
Basic Stash Operations
Stash Commands
Basic Stash Operations
# Basic Stash Operations
# 1. Stash current changes
git stash
# 2. Stash with message
git stash push -m "WIP: working on feature"
# 3. Stash specific files
git stash push -m "WIP: config changes" config/database.yml
# 4. Stash including untracked files
git stash push -u -m "WIP: including untracked files"
# 5. Stash including ignored files
git stash push -a -m "WIP: including ignored files"
# 6. List stashes
git stash list
# 7. Show stash contents
git stash show
# 8. Show stash diff
git stash show -p
# 9. Apply stash (keep stash)
git stash apply
# 10. Apply specific stash
git stash apply stash@{2}
# 11. Pop stash (remove from stash)
git stash pop
# 12. Pop specific stash
git stash pop stash@{1}
# 13. Drop stash
git stash drop
# 14. Drop specific stash
git stash drop stash@{0}
# 15. Clear all stashes
git stash clear
Advanced Stash Workflow
Stash Management Strategies
Advanced Stash Workflow
# Advanced Stash Workflow
# 1. Stash workflow for feature switching
# Working on feature A
git add .
git stash push -m "WIP: feature A - user authentication"
# Switch to urgent bug fix
git checkout main
git checkout -b hotfix/critical-bug
# Fix bug and commit
git add .
git commit -m "Fix critical bug"
git checkout main
git merge hotfix/critical-bug
# Return to feature A
git checkout feature/user-auth
git stash pop
# 2. Stash workflow for pull requests
# Working on feature
git add .
git stash push -m "WIP: feature implementation"
# Pull latest changes
git checkout main
git pull origin main
# Update feature branch
git checkout feature/new-feature
git rebase main
# Apply stashed changes
git stash pop
# Resolve conflicts if any
# Continue development
# 3. Stash workflow for experiments
# Save current work
git stash push -m "WIP: current implementation"
# Create experimental branch
git checkout -b experiment/new-approach
# Try new approach
# ... make changes ...
# If experiment fails, return to original
git checkout feature/current-feature
git stash pop
# If experiment succeeds, merge
git checkout feature/current-feature
git merge experiment/new-approach
# 4. Stash workflow for code reviews
# Working on feature
git add .
git stash push -m "WIP: feature ready for review"
# Create review branch
git checkout -b review/feature-name
# Apply stashed changes
git stash pop
# Commit and push for review
git add .
git commit -m "Feature implementation"
git push origin review/feature-name
# Return to development
git checkout feature/current-feature
git stash push -m "WIP: post-review changes"
# 5. Stash workflow for testing
# Working on feature
git add .
git stash push -m "WIP: feature implementation"
# Switch to test branch
git checkout test/feature-tests
# Apply changes for testing
git stash pop
# Run tests
npm test
# If tests pass, merge to feature
git checkout feature/current-feature
git merge test/feature-tests
# If tests fail, return to development
git checkout feature/current-feature
git stash pop
Stash Best Practices
Workflow Guidelines
Stash Best Practices
# Stash Best Practices
# 1. Use descriptive messages
git stash push -m "WIP: user authentication - login form"
# 2. Stash frequently
# Before switching branches
git stash push -m "WIP: current work"
# Before pulling changes
git stash push -m "WIP: before pull"
# Before rebasing
git stash push -m "WIP: before rebase"
# 3. Organize stashes by purpose
git stash push -m "WIP: feature/user-auth - login logic"
git stash push -m "WIP: feature/user-auth - validation"
git stash push -m "WIP: feature/user-auth - error handling"
# 4. Use stash for temporary changes
# Experimental code
git stash push -m "WIP: experimental approach"
# Debug code
git stash push -m "WIP: debug logging"
# Temporary fixes
git stash push -m "WIP: temporary workaround"
# 5. Clean up stashes regularly
# List stashes
git stash list
# Remove old stashes
git stash drop stash@{5}
git stash drop stash@{4}
# Clear all stashes
git stash clear
# 6. Use stash with branches
# Stash before branch switch
git stash push -m "WIP: current work"
git checkout other-branch
# Apply stash in new branch
git stash pop
# 7. Stash partial changes
# Stash specific files
git stash push -m "WIP: config changes" config/
# Stash by pattern
git stash push -m "WIP: test files" "*.test.js"
# 8. Stash with untracked files
# Include untracked files
git stash push -u -m "WIP: including new files"
# Include ignored files
git stash push -a -m "WIP: including ignored files"
# 9. Stash for collaboration
# Stash before sharing
git stash push -m "WIP: local changes"
# Share branch
git push origin feature-branch
# Apply stash after pull
git pull origin feature-branch
git stash pop
# 10. Stash for deployment
# Stash before deployment
git stash push -m "WIP: local changes"
# Deploy from clean state
git checkout main
git pull origin main
# Return to development
git checkout feature-branch
git stash pop
Stash Troubleshooting
Common Issues and Solutions
Stash Issues
- Stash conflicts on apply
- Lost stashes
- Stash merge conflicts
- Stash corruption
- Stash size issues
- Stash performance
- Stash cleanup
Solutions
- Resolve conflicts manually
- Use reflog to recover
- Apply stash in stages
- Recreate corrupted stash
- Split large stashes
- Optimize stash operations
- Regular cleanup routine
Summary
Git stash workflow involves several key components:
- Basic Operations: Stash, apply, pop, drop commands
- Advanced Workflow: Feature switching, pull requests, experiments
- Best Practices: Descriptive messages, frequent stashing, cleanup
- Troubleshooting: Common issues and solutions
Need More Help?
Struggling with Git stash workflow or need help managing temporary changes? Our Git experts can help you implement effective stashing strategies.
Get Git Stash Help