How can I compare the current working tree with changes in the Git stash to preview what will be applied when un-stashing?
To compare the current working tree with changes in a Git stash and preview what will be applied when un-stashing, you can use several commands including git stash show -p to see the stash contents as a patch, git diff stash@{n} to compare directly with your working directory, or create a temporary branch to safely examine the differences before applying any changes.
Contents
- Basic Stash Preview Methods
- Comparing Working Tree with Stash
- Advanced Stash Inspection Techniques
- Practical Examples and Workflows
- Best Practices for Stash Management
Basic Stash Preview Methods
The simplest way to preview what’s in your Git stash is using the git stash show command. This command displays the changes recorded in the stash as a diff between the stashed state and its original parent commit.
# Show all stashed changes in summary format
git stash show
# Show the most recent stash (stash@{0}) in patch format
git stash show -p stash@{0}
The -p or --patch flag is particularly useful as it shows the actual changes line by line, allowing you to see exactly what would be applied to your working tree when you un-stash source.
For a more comprehensive overview of all your stashed changes, use git stash list:
# List all stashes with their identifiers
git stash list
This command displays each stash with its corresponding identifier (like stash@{0}, stash@{1}, etc.) and description, helping you identify which stash you want to examine source.
Comparing Working Tree with Stash
To see what changes un-stashing will make to your current working tree, you need to compare the stash with your current working directory state. Here are the most effective methods:
Method 1: Using git diff with stash
# Compare working tree with the most recent stash
git diff stash@{0}
# Compare working tree with a specific stash
git diff stash@{1}
This command shows the differences between your current working directory and the changes that would be applied if you were to apply the specified stash source.
Method 2: Creating a temporary branch
A safer approach is to create a branch from your stash and then compare it with your current branch:
# Create a branch from the stash
git stash branch my-stash-branch stash@{0}
# Compare the new branch with your current branch
git diff my-stash-branch..main
This method allows you to examine the changes without affecting your working directory. You can simply delete the branch after you’re done reviewing source.
Method 3: Using tig for interactive viewing
If you have tig installed, you can use it to interactively view your stashes:
# Open tig in stash view mode
tig stash
This provides a more visual and interactive way to examine your stashed changes source.
Advanced Stash Inspection Techniques
For more sophisticated stash inspection, you can use several Git commands in combination:
Viewing stash changes with specific file paths
# Show changes for a specific file in the stash
git stash show -p stash@{0} -- path/to/file.txt
# Compare working tree with stash for a specific file
git diff stash@{0} -- path/to/file.txt
Using git show to examine stash commits
Each stash is actually stored as a commit in Git’s internal stash reference. You can examine it directly:
# Show the stash commit details
git show stash@{0}
# Show the stash commit as patch
git show -p stash@{0}
Interactive stash selection
Git offers interactive selection for stashing changes:
# Interactively select hunks from the diff between HEAD and working tree
git stash push -p
This allows you to selectively choose which changes to stash, making it easier to manage related changes source.
Practical Examples and Workflows
Here are some practical workflows for comparing working tree with stash:
Workflow 1: Safe stash inspection
# 1. Check what's in your current working directory
git status
# 2. List available stashes
git stash list
# 3. Preview the most recent stash changes
git stash show -p stash@{0}
# 4. Compare with current working tree
git diff stash@{0}
# 5. If satisfied, apply the stash
git stash pop
Workflow 2: Multiple stash comparison
# 1. List all stashes
git stash list
# 2. Preview each stash before deciding which to apply
for i in {0..2}; do
echo "=== Stash @{${i}} ==="
git stash show -p stash@{${i}}
echo ""
done
# 3. Compare the stash you want with current working tree
git diff stash@{1}
# 4. Apply the selected stash
git stash apply stash@{1}
Workflow 3: Branch-based stash comparison
# 1. Create a branch from your stash
git stash branch feature-branch stash@{0}
# 2. Checkout the new branch
git checkout feature-branch
# 3. Review the changes in isolation
git diff main
# 4. Make any necessary adjustments
# 5. Merge back to main when ready
git checkout main
git merge feature-branch
# 6. Delete the temporary branch
git branch -d feature-branch
Best Practices for Stash Management
Organizing your stashes
Use descriptive messages when creating stashes to make them easier to identify later:
# Create stash with descriptive message
git stash push -m "WIP: Add user authentication feature"
# List stashes with messages
git stash list
Regular stash cleanup
Over time, you may accumulate many stashes. Regularly clean them up:
# Drop specific stash
git stash drop stash@{2}
# Drop all stashes
git stash clear
Backup important stashes
For important work that you want to preserve, consider creating a branch instead of relying solely on stashes:
# Create branch from stash and keep stash
git stash branch important-work stash@{0}
Integration with GUI tools
Many GUI Git clients provide visual stash management. Tools like GitKraken, Sourcetree, or the built-in Git integration in IDEs like VS Code often have stash panels that allow you to preview changes with a click source.
Sources
- Git - git-stash Documentation
- How to preview the changes that “git stash apply” will make? - Stack Overflow
- How to See Changes in Git Before Un-stashing Them - Medium
- See What’s in a Stash without Applying It? - Better Stack Community
- git stash - Saving Changes - Atlassian Git Tutorial
- How to compare a git stash to the current working tree? - Software Development
- How to view the diff of a Git stash - Graphite
- Comparing Git Stash with Working Tree - Programming
Conclusion
Previewing Git stash changes before applying them is essential for maintaining code quality and avoiding unintended conflicts. The key methods include:
- Use
git stash show -pto examine stash contents as a patch - Employ
git diff stash@{n}to compare directly with your working directory - Create temporary branches with
git stash branchfor safe inspection - Leverage tools like
tig stashfor interactive viewing
By mastering these techniques, you can confidently manage your stashed changes, understand exactly what will be applied to your working tree, and maintain clean, organized version control workflows. Remember to use descriptive stash messages and regularly clean up old stashes to keep your repository manageable.