How to compare changes between the working tree and the stash using git diff?
How can I preview the changes that will be applied to the current working tree when un-stashing? I want to see what modifications will be made before actually applying them from the stash.
You can compare changes between your working tree and stash using several Git commands, with the most common approaches being git stash show -p to view patch-formatted changes in a stash, and git diff HEAD stash@{0} to compare your current working tree state against what was stashed. These methods allow you to preview exactly what modifications will be applied before actually un-stashing, helping you avoid unwanted changes to your working directory.
Contents
- Understanding Git Stash Comparison
- Method 1: Using git stash show
- Method 2: Using git diff with Stash References
- Method 3: IDE and GUI Tools
- Advanced Techniques and Best Practices
- Common Scenarios and Examples
Understanding Git Stash Comparison
When working with Git, stashing allows you to temporarily save changes without committing them. However, before applying these changes back to your working tree, it’s crucial to understand what modifications will be made. This is where comparison techniques become essential.
The Git documentation explains that stashes store both the working tree state and the index, making them powerful for preserving complete work-in-progress. When you want to preview changes, you need to compare either:
- The stash contents against your current working tree
- The stash contents against the commit where it was created
- The current working tree against what applying the stash would produce
Understanding these comparisons helps prevent conflicts and ensures you’re applying the right changes in the right context.
Method 1: Using git stash show
The git stash show command is the primary tool for examining stash contents. By default, it displays a summary of changes, but you can use the -p (or --patch) option to see the actual diff.
Basic Usage
git stash show -p
git stash show -p stash@{0} # Show specific stash
git stash show -p stash@{1} # Second most recent stash
This command shows the changes that would be applied if you were to un-stash, displayed in the standard Git diff format. According to Stack Overflow, this is the most straightforward way to preview stash changes.
Enhanced Viewing Options
You can redirect the output to a text editor for better readability:
git stash show -p stash@{0} > ~/.diff && vim ~/.diff
This approach is particularly useful when dealing with complex changes that benefit from syntax highlighting and navigation features in modern text editors.
Understanding the Output
The -p option shows:
- Added lines with
+prefix - Removed lines with
-prefix - File modification context
- Unified diff format for easy reading
This gives you a complete picture of what changes would be applied to your working tree.
Method 2: Using git diff with Stash References
Another powerful approach is using git diff with stash references to compare different states. This method provides more flexibility in comparison.
Comparing Working Tree with Stash
git diff HEAD stash@{0}
git diff stash@{0} # Same as above if HEAD is implied
As explained in this Medium article, this command compares your current HEAD (working tree) with the stashed changes, showing you exactly what differences exist between these two states.
Comparing Stash Against its Original Commit
git diff stash@{0}^ stash@{0}
This shows the changes that were originally stashed, helping you understand what was saved.
Reverse Comparison
git diff stash@{0} HEAD
This shows what would change if you applied the stash to your current working tree - essentially the reverse of the first command.
Multiple Stash Comparison
You can compare multiple stashes:
git diff stash@{0} stash@{1}
This is useful when you have multiple stashes and want to see the differences between them.
Method 3: IDE and GUI Tools
Many modern Git IDEs and GUI tools provide visual stash comparison interfaces that can be more intuitive than command-line diff output.
Visual Studio Code
- Open the Source Control panel
- Click on the “Stashes” section
- Click on any stash entry to see a side-by-side diff
- You can preview and apply changes directly from the interface
PyCharm and IntelliJ IDEA
- Use the Git tool window
- Navigate to the Stashes section
- Right-click on a stash and select “Show Diff”
- You can also create a new branch from a stash if needed
Visual Studio (Windows)
As mentioned in Stack Overflow discussions, you can double-click stash entries in the Git Changes window to preview them.
GitKraken and Other GUI Tools
- Most modern Git GUIs have dedicated stash views
- These typically show diffs in a more visual format
- Some allow you to selectively apply parts of a stash
The advantage of GUI tools is that they often provide:
- Color-coded diff output
- Side-by-side comparison
- Easy navigation through changes
- Ability to selectively apply parts of a stash
Advanced Techniques and Best Practices
Previewing with Temporary Application
Sometimes the best way to understand what changes will be made is to temporarily apply them:
# Apply stash without removing it
git stash apply stash@{0}
# Check what changed
git diff
git diff --cached
# If you don't want the changes
git reset --hard # Only if you haven't committed
git checkout . # Discard working tree changes
# Remove the temporary application
git stash drop stash@{0}
Using --index Flag
git stash apply --index stash@{0}
This command applies the stash and also restores the index state, which can be useful for understanding how staged changes would be affected.
Scripting for Multiple Stashes
You can create scripts to preview multiple stashes systematically:
#!/bin/bash
echo "Listing available stashes..."
git stash list
echo "Choose a stash to preview changes from (e.g., stash@{0}):"
read STASH_NAME
echo "Previewing changes for $STASH_NAME"
git stash show -p $STASH_NAME
echo "Apply changes temporarily to inspect? (y/n)"
read APPLY
if [ "$APPLY" = "y" ]; then
git stash apply $STASH_NAME
echo "Changes applied. Press Enter to undo and remove stash..."
read
git reset --hard
git stash drop $STASH_NAME
fi
Fuzzy Finding for Stash Management
For those who use fuzzy finders like fzf, you can create enhanced stash management:
# Set up Alt+P to pop selected stash
# From research on managing Git stashes with fzf
Common Scenarios and Examples
Scenario 1: Checking What a Stash Contains
# List stashes
git stash list
# Preview the most recent stash
git stash show -p stash@{0}
# Preview a specific stash by name
git stash show -p "my-feature-changes"
Scenario 2: Comparing Current Work with Stashed Changes
# See what differs between current working tree and stash
git diff HEAD stash@{0}
# See what would change if applied (reverse view)
git diff stash@{0} HEAD
Scenario 3: Safely Applying a Stash
# 1. Preview changes first
git stash show -p stash@{0}
# 2. Apply without removing from stash
git stash apply stash@{0}
# 3. Check if changes are correct
git status
git diff
# 4. If correct, remove the stash
git stash drop stash@{0}
# 5. If not correct, undo the apply
git reset --hard # Only if you haven't made additional changes
Scenario 4: Multiple Branch Context
When working with multiple branches, it’s crucial to understand where a stash was created:
# Check where stash was created
git stash show -p stash@{0} --stat
# Compare stash with current branch
git diff stash@{0} HEAD
# Compare stash with original branch (if known)
git diff stash@{0} origin/feature-branch
Conclusion
Previewing changes between your working tree and stash is essential for safe Git workflow management. The key methods include:
- Using
git stash show -pfor direct patch viewing of stashed changes - Using
git diff HEAD stash@{0}to compare current working tree with stashed modifications - Leveraging IDE tools for visual diff interfaces when available
- Temporarily applying changes when you need to see the actual effect on your working directory
Best practices recommend always previewing stash changes before applying them, especially when working on complex projects or when stashes contain changes that might conflict with current work. The combination of these techniques gives you full control over your stashed changes and prevents unexpected modifications to your working tree.
Remember that stashes are local to your repository - they won’t be transferred when you push to remote repositories, making them perfect for temporary work preservation.
Sources
- How to “git diff” the working tree to the stash? - Stack Overflow
- 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
- git stash - Saving Changes | Atlassian Git Tutorial
- Git Basics: Diff and Stash - This Dot Labs
- Can git do a diff of the working copy with stash - Stack Overflow
- How to list and show the git stash history
- How to compare a git stash to the current working tree? - Software Development
- Shelve or stash changes | IntelliJ IDEA Documentation