What is a Git stash?
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.
Basic usage of git stash
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:
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.
Viewing the diff of a stash
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:
Diff between stash and current working directory: To see what has changed from your working directory since you stashed changes, you can use:
Terminalgit diff stash@{0}Here,
stash@{0}
refers to the latest stash. Git uses a stack to manage stashes, sostash@{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:
Terminaldiff --git a/file1.txt b/file1.txtindex 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 directoryOther unchanged textAnother line of unchanged textMore unchanged textdiff --git a/file1.txt b/file1.txt
: This line indicates that the diff is forfile1.txt
. The formata/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/
).index 1234567..89abcde 100644
: This line shows metadata about the file versions being compared.1234567
and89abcde
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.--- a/file1.txt
: This denotes the version of the file from the stash (the "old" version).+++ b/file1.txt
: This denotes the version of the file from the current working directory (the "new" version).@@ -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.-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.+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.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.
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:
Terminalgit diff stash@{0}^1 stash@{0}This compares the parent commit of the stash (
stash@{0}^1
) with the stash itself.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:
Terminalgit diff stash@{0} -- path/to/file
For further reading on stashing in Git, see the official documentation.