Data report"State of code review 2024" is now liveRead the full report

How to view the diff of a Git stash

Greg Foster
Greg Foster
Graphite software engineer


Note

This guide explains this concept in vanilla Git. For Graphite documentation, see our CLI docs.


A stash in Git is used to save uncommitted changes, including staged and unstaged modifications, without committing them to the repository's history. This can be particularly useful when you need to quickly switch context and work on something else, but you're not ready to commit your current changes.

This guide will show you how to view the differences in a stash, helping you keep track of changes and manage your work effectively.

Before diving into viewing diffs, it's essential to know how to create a stash. To save your current changes to a stash, you can use:

Terminal
git stash push -m "Message describing the stash"

The -m option allows you to provide a descriptive message for the stash, making it easier to identify later.

Once you have stashed changes, you may want to view the differences between your stash and other commits or the current working tree. Here’s how you can do it:

  1. Diff between stash and current working directory: To see what has changed from your working directory since you stashed changes, you can use:

    Terminal
    git diff stash@{0}

    Here, stash@{0} refers to the latest stash. Git uses a stack to manage stashes, so stash@{0} is the most recent stash, stash@{1} is the one before that, and so on.

    After running this command you should see output like this:

    Terminal
    diff --git a/file1.txt b/file1.txt
    index 1234567..89abcde 100644
    --- a/file1.txt
    +++ b/file1.txt
    @@ -1,4 +1,4 @@
    -This is the original text in the stash
    +This is the new text in the working directory
    Other unchanged text
    Another line of unchanged text
    More unchanged text
    1. diff --git a/file1.txt b/file1.txt: This line indicates that the diff is for file1.txt. The format a/file1.txt b/file1.txt shows the comparison between two versions of the file: the version from the stash (a/) and the version in the current working directory (b/).

    2. index 1234567..89abcde 100644: This line shows metadata about the file versions being compared. 1234567 and 89abcde are abbreviated Git SHA-1 hashes representing the file states before and after the changes. 100644 indicates the file mode, which in this case tells us that it's a normal file.

    3. --- a/file1.txt: This denotes the version of the file from the stash (the "old" version).

    4. +++ b/file1.txt: This denotes the version of the file from the current working directory (the "new" version).

    5. @@ -1,4 +1,4 @@: This is a hunk header. It shows which lines of the old file (-1,4) and the new file (+1,4) are being displayed in this section of the diff. The format is @@ -startline, numberoflines +startline, numberoflines @@. It's showing that the hunk starts at line 1 and includes four lines in both the old and new versions.

    6. -This is the original text in the stash: Lines beginning with - represent lines that were in the stash but are not in the current working directory. Here, "This is the original text in the stash" has been removed or changed in the current working directory.

    7. +This is the new text in the working directory: Lines beginning with + represent lines that are in the current working directory but were not in the stash. Here, "This is the new text in the working directory" has been added or changed compared to what was stashed.

    8. Lines that are not prefixed with + or - are unchanged between the stash and the current working directory. They are included to provide context around the changes.

    This output helps developers understand exactly what has been modified in their working directory since the last time they saved their changes to the stash. This is particularly useful for reconciling changes and ensuring that no important modifications are lost.

  2. Diff between stash and its original parent commit: If you want to see the changes that were stashed as compared to the commit that was HEAD at the time the stash was created, you can use:

    Terminal
    git diff stash@{0}^1 stash@{0}

    This compares the parent commit of the stash (stash@{0}^1) with the stash itself.

  3. Diff specific files in a stash: To view changes for specific files within a stash, specify the file path at the end of the command:

    Terminal
    git diff stash@{0} -- path/to/file

For further reading on stashing in Git, see the official documentation.

Stay unblocked. Ship faster.
Experience the new developer workflow - create, review, and merge code continuously. Get started with one command.
Learn more

Give your PR workflow
an upgrade today

Stack easier | Ship smaller | Review quicker

Or install our CLI.
Product Screenshot 1
Product Screenshot 2