Git Diff against a Stash
In Git, you can compare your current working directory or a specific branch against a stash. This can be useful to see what changes were stashed away. Here are the steps to do so:
Method 1: Diff Against the Most Recent Stash
To compare the working directory with the most recent stash entry:
git stash show -p
This will show the differences between the current working directory and the most recent stash entry.
Method 2: Diff Against a Specific Stash
If you have multiple stashes and want to compare against a specific one, you'll need to refer to the stash by its index or name.
List all stashes:
git stash list
This will output something like:
stash@{0}: WIP on branch-name: commit-message stash@{1}: WIP on another-branch: another-commit-message
Show the diff for a specific stash:
git stash show -p stash@{n}
Replace
stash@{n}
with the specific stash you want to compare against. For example, to compare against the second stash:git stash show -p stash@{1}
Method 3: Diff Against a Specific File in a Stash
If you want to compare a specific file against the version in a stash:
git diff stash@{n} -- path/to/file
Replace stash@{n}
with the specific stash reference and path/to/file
with the file path you want to compare.
Method 4: Creating a Branch from a Stash
For a more detailed comparison or to work on the stashed changes, you can create a new branch from the stash:
Create a new branch from the stash:
git stash branch my-stash-branch stash@{n}
This will apply the stash to a new branch called
my-stash-branch
. Replacestash@{n}
with the specific stash reference.Diff between the branches:
git diff my-stash-branch..main
Replace
main
with the branch you want to compare against.
Summary
- Diff against the most recent stash:
git stash show -p
- Diff against a specific stash:
git stash show -p stash@{n}
- Diff a specific file against a stash:
git diff stash@{n} -- path/to/file
- Create a branch from a stash and diff:
git stash branch my-stash-branch stash@{n}
followed bygit diff my-stash-branch..main
These methods should help you efficiently compare your working directory or specific files against stashed changes.
Make your mark
Join the writer's program
Are you a developer and love writing and sharing your knowledge with the world? Join our guest writing program and get paid for writing amazing technical guides. We'll get them to the right readers that will appreciate them.
Write for us![](/assets/articles/authors/marin-0d19fe482115fe2b15c8429a7ee4eb88d803984eaa7c1d8c7309241049f3d402.png)
Build on top of Better Stack
Write a script, app or project on top of Better Stack and share it with the world. Make a public repository and share it with us at our email.
community@betterstack.comor submit a pull request and help us build better products for everyone.
See the full list of amazing projects on github