Git
How to Use Git Stash?
When working with Git, you may find yourself in situations where you need to switch branches or pull updates but have uncommitted changes in your working directory. Committing incomplete changes may not be ideal, and discarding them risks losing progress. This is where Git stash comes in handy.
Git stash allows you to temporarily store your uncommitted changes, switch contexts, and later restore them seamlessly. In this blog, we’ll dive into what Git stash is, its common use cases, and how to leverage it effectively.
What is Git Stash?
Git stash is a command that saves your working directory’s state (tracked files, staged changes, and optionally untracked files) in a stack-like structure. This lets you switch branches, pull changes, or perform other operations without losing your current progress.
Think of it as a “clipboard” for your code — you can cut (stash) and later paste (apply) changes when needed.
Common Use Cases for Git Stash
- Switching Branches: Temporarily save changes while moving to another branch.
- Pulling Updates: Stash changes before pulling updates from the remote repository to avoid conflicts.
- Experimentation: Test an idea without committing to the current branch.
- Cleanup Workflow: Temporarily save changes to focus on debugging or reviewing specific parts of the codebase.
How to Use Git Stash
1. Stashing Changes
To stash your current working directory changes:
git stash
This command saves all tracked and staged changes to the stash stack and restores your working directory to a clean state (matching the last commit).
2. Viewing the Stash List
To see a list of all stashed changes:
git stash list
Example Output:
stash@{0}: WIP on main: abc1234 Add feature X
stash@{1}: WIP on main: def5678 Fix bug Y
Each entry shows:
- The stash identifier (
stash@{n}
) - The branch it was created on
- The commit hash and message of the associated commit
3. Applying Stashed Changes
To reapply the most recent stash:
git stash apply
If you want to apply a specific stash:
git stash apply stash@{n}
Note: Applying a stash does not remove it from the stack. Use
git stash drop
to delete it after application if it’s no longer needed.
4. Stashing and Removing Changes Simultaneously
To stash and remove changes from the stash stack in one step:
git stash pop
This applies the most recent stash and removes it from the stack.
5. Stashing Untracked and Ignored Files
By default, Git stash only saves changes to tracked files. To include untracked files:
git stash -u
To include both untracked and ignored files:
git stash -a
6. Deleting a Stash
To delete a specific stash:
git stash drop stash@{n}
To delete all stashed changes:
git stash clear
Practical Examples
Scenario 1: Switching Branches
- You’ve made changes on the
main
branch but need to switch tofeature-branch
. - Stash your changes:
git stash
- Switch branches:
git checkout feature-branch
- Return to the
main
branch and apply the stash when ready:git checkout main git stash apply
Scenario 2: Resolving Pull Conflicts
- You’ve made changes locally but need to pull updates from the remote repository.
- Stash your changes:
git stash
- Pull the updates:
git pull origin branch-name
- Reapply your changes:
git stash pop
Best Practices with Git Stash
- Use Descriptive Commit Messages: Before stashing, consider committing your changes with a meaningful message to avoid confusion.
- Track Stashes: Use
git stash list
regularly to ensure you don’t lose track of saved changes. - Minimize Stash Overload: Clear stashes you no longer need to avoid clutter.
- Work Incrementally: Stash small, related changes together rather than large, unrelated modifications.
- Verify Before Applying: Use
git stash show stash@{n}
to preview a stash’s changes before applying.
Common Issues and Solutions
1. Stashed Changes Missing
- Cause: The stash stack was cleared or not saved.
- Solution: Verify stashes immediately using
git stash list
. Use commits instead of stashes for long-term storage.
2. Conflicts When Applying a Stash
- Cause: The changes in the stash conflict with the current state of the branch.
- Solution: Resolve conflicts manually, and then stage the resolved files using
git add
.
3. Accidentally Deleted Stash
- Cause: Using
git stash drop
orgit stash clear
prematurely. - Solution: Unfortunately, stash deletions are irreversible. Use commits for critical changes.
Conclusion
Git stash is a versatile and powerful tool that helps developers manage their workflows efficiently. By understanding how to stash, apply, and manage stashed changes, you can handle interruptions and multitask without risking your progress.
Incorporate Git stash into your daily Git workflow, and you’ll soon find it invaluable for maintaining a clean and organized development process.