Stashing Changes in Git
Git stash provides a way to temporarily store your working changes without committing them. This guide will show you how to effectively use stashing in your Git workflow.
What You’ll Learn
- Basic stashing
- Managing stashes
- Applying stashes
- Best practices
- Common scenarios
Implementation Steps
-
Basic Stashing
# Stash all changes git stash save "WIP: feature in progress" # Stash with description git stash push -m "Work in progress" # Stash specific files git stash push path/to/file1 path/to/file2
- Save changes
- Add messages
- Select files
- Verify stash
-
View Stashes
# List all stashes git stash list # Show stash contents git stash show -p stash@{0} # View stash diff git stash show -p
- Check stash list
- View contents
- Compare changes
- Track stash
-
Apply Stashes
# Apply latest stash git stash apply # Apply specific stash git stash apply stash@{2} # Apply and remove git stash pop
- Apply changes
- Select stash
- Remove after apply
- Verify changes
-
Manage Stashes
# Drop specific stash git stash drop stash@{1} # Clear all stashes git stash clear # Create branch from stash git stash branch new-branch stash@{0}
- Remove stashes
- Clean up
- Create branches
- Organize work
Best Practices
-
Before Stashing
- Review changes
- Add descriptions
- Check status
- Plan workflow
-
During Stashing
- Use clear messages
- Select relevant files
- Track stash list
- Test after apply
-
After Stashing
- Verify changes
- Clean up old stashes
- Update documentation
- Monitor conflicts
-
Stash Organization
- Name stashes clearly
- Group related changes
- Regular cleanup
- Document purpose
Common Use Cases
-
Switch Branches
# Stash changes git stash save "WIP: feature" # Switch branch git checkout main # Return and apply git checkout feature git stash pop
-
Pull Updates
# Stash changes git stash # Pull updates git pull # Apply stash git stash pop
-
Emergency Fix
# Stash current work git stash save "WIP: main feature" # Fix emergency git checkout hotfix # ... fix and commit ... # Return to work git checkout feature git stash pop
-
Clean Working Directory
# Stash all changes git stash --include-untracked # Clean directory git clean -fd # Restore when needed git stash pop
Advanced Usage
-
Selective Stashing
# Stash specific changes git stash push -p # Stash untracked files git stash --include-untracked
-
Stash Branching
# Create branch from stash git stash branch new-feature stash@{0}
-
Stash Patches
# Create stash patch git stash show -p > changes.patch # Apply patch git apply changes.patch
-
Stash with Message
# Stash with description git stash push -m "Feature in progress"
Common Issues and Solutions
-
Merge Conflicts
# Resolve conflicts git stash pop # ... resolve conflicts ... git add . git commit -m "Resolve stash conflicts"
-
Lost Stash
# Find lost stash git fsck --unreachable | grep commit git show <commit-hash>
-
Stash Application
# Apply with conflicts git stash apply --3way
Conclusion
Stashing is a powerful tool for managing work in progress. Remember to:
- Use clear messages
- Organize stashes
- Clean up regularly
- Follow best practices
- Plan workflow
Next Steps
After mastering stashing, consider:
- Learning about Git workflows
- Exploring Git hooks
- Understanding Git internals
- Setting up automated stashing