Git & GitHub - Git Stash
How to use git stash to save changes
Git stash is a useful feature that allows you to temporarily save changes in your working directory without committing them. This guide covers how to use git stash effectively, including stashing changes, applying stashes, and managing the stash list.
Key Points:
- Git stash saves your uncommitted changes and allows you to work on something else.
- You can apply stashed changes later, either partially or completely.
- Stashes are stored in a stack, and you can manage multiple stashes at once.
Stashing Changes
Basic Stash
To stash your changes, use the git stash
command:
# Stash your changes
$ git stash
Stash with Message
You can add a message to your stash to describe the changes:
# Stash your changes with a message
$ git stash save "WIP: Working on feature X"
Stash Untracked Files
To include untracked files in the stash, use the -u
option:
# Stash your changes including untracked files
$ git stash -u
Applying Stashes
Apply the Latest Stash
To apply the latest stash, use the git stash apply
command:
# Apply the latest stash
$ git stash apply
Apply a Specific Stash
To apply a specific stash, use the git stash list
command to find the stash index, then apply it:
# List all stashes
$ git stash list
# Apply a specific stash
$ git stash apply stash@{2}
Pop the Latest Stash
To apply and remove the latest stash from the stack, use the git stash pop
command:
# Apply and remove the latest stash
$ git stash pop
Managing Stashes
Viewing Stashes
Use the git stash list
command to view all stashes:
# List all stashes
$ git stash list
Dropping a Stash
To remove a specific stash, use the git stash drop
command followed by the stash index:
# Drop a specific stash
$ git stash drop stash@{2}
Clearing All Stashes
To remove all stashes, use the git stash clear
command:
# Clear all stashes
$ git stash clear
Using Stash in Real-World Scenarios
Switching Branches
Stash your changes before switching branches to avoid conflicts and preserve your work:
# Stash changes before switching branches
$ git stash
$ git checkout other-branch
# Apply the stash when returning to the original branch
$ git checkout original-branch
$ git stash pop
Temporary Work
Use stash to save your work temporarily if you need to work on something else urgently:
# Stash changes to work on an urgent task
$ git stash save "WIP: Current task"
# Work on the urgent task
$ git checkout urgent-task-branch
# Return to your work and apply the stash
$ git checkout original-branch
$ git stash pop
Best Practices
Follow these best practices when using git stash:
- Use Descriptive Messages: Add descriptive messages to your stashes to remember the context of your changes.
- Review Stashes Regularly: Regularly review and clean up stashes to keep your repository organized.
- Handle Conflicts Carefully: Conflicts can occur when applying stashes; handle them carefully to ensure code integrity.
Summary
This guide covered how to use git stash to save changes, including stashing, applying stashes, managing stashes, real-world use cases, and best practices. Git stash is a powerful tool that helps you manage your uncommitted changes effectively, allowing you to switch contexts and manage your work more efficiently.