Git Diff against a Stash

Better Stack Team
Updated on July 25, 2024

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.

  1. 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
    
  2. 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:

  1. 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. Replace stash@{n} with the specific stash reference.

  2. 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 by git diff my-stash-branch..main

These methods should help you efficiently compare your working directory or specific files against stashed changes.

Got an article suggestion? Let us know
Explore more
Git
Licensed under CC-BY-NC-SA

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.