Learn how to handle incomplete work and urgent tasks seamlessly using git stash and git stash pop in this practical guide. These commands allow you to temporarily save your uncommitted changes, switch branches, and retrieve your work without creating unnecessary commits.
Prerequisites for Using Git Stash
Before diving into using git stash, ensure you meet the following requirements:
prerequisites
- Git is installed and configured on your computer.
- You have an initialized Git repository with some uncommitted changes.
- Basic knowledge of Git commands like
git status,git branch, andgit switch.
Stash Changes in Git
You can use git stash to save your uncommitted changes without committing them to your repository. Follow these steps:
steps
- Check the status of your working directory:bash
git status - To stash changes (staged and unstaged):
git stash # Temporarily save changes
# Output example:
# Saved working directory and index state WIP on branch_name: <commit_hash> <message>- To include untracked files, use:
git stash -u
# Output example:
# Saved working directory and index state WIP on branch_name: <commit_hash> <message>- Verify the stashes saved:
git stash list
# Output example:
# stash@{0}: WIP on branch_name: <commit_hash> <message>Retrieve Stashed Changes Using Git Stash Pop
Once you're ready to continue where you left off, use git stash pop to apply the latest stash.
steps
- Switch back to the branch where you want to apply the changes:bash
git switch branch_name - Apply and remove the most recent stash:
git stash pop
# Output example:
# Auto-merging filename
# Dropped stash@{0- If conflicts arise, resolve them as you would during a merge conflict:bash
git status # Identify conflict files git mergetool # Use a merge tool to resolve conflicts
Optional Commands for Advanced Usage
Expand your git stash capabilities with these advanced commands:
steps
- Apply a stash without removing it:
git stash apply
# Changes applied but stash remains- Apply a specific stash using its identifier:
git stash apply stash@{1}
# Applies the second stash in the list- Remove a specific stash:
git stash drop stash@{1}
# Removes the specified stash- Clear all stashes:
git stash clear
# Removes all stashes from historyCommon Pitfalls with Git Stash
When working with git stash, beware of these common issues:
Confirming Your Stash Operations
After each operation, you can verify its results to ensure your changes are saved or restored correctly.
steps
- Check the list of stashes:
git stash list- Confirm retrieved changes are back in your working directory:bash
git status
Summary of Git Stash Operations
FAQ
How can I include untracked files in a stash?
Use the git stash -u command to include untracked files in the stash.
What is the difference between git stash pop and git stash apply?
git stash pop applies the changes from the stash and simultaneously removes the stash. In contrast, git stash apply applies the changes but retains the stash for future use.
What happens during a stash conflict?
Stash conflicts occur when changes in the stash conflict with changes in the working directory or branch. Git will notify you of the conflict, and you'll need to resolve it manually, similar to resolving merge conflicts.
Can I view or restore a specific stash?
Yes, use git stash list to view all stashes and retrieve specific ones using git stash pop stash@{n} or git stash apply stash@{n}.
Official reference: Git reference manual.