Stash operation in git allows you to save the local changes on your system without losing.
This is useful when you want to pull latest changes from remote repository or for switching branches.

To create a stash you can use git stash command along with its multiple options.
In this article, we will understand how to see the contents of a stash without applying it.
We will go through both the options where you can see not only see the list of files in a stash but also view the changes in each of those files.

List all stash

You can list all stashes on your local system using command

git stash list

This will display a list of all stashes without applying or popping as below

stash@{0}: WIP on master edacb58
stash@{1}: WIP on master 1efsb20

where 0 and 1 are indexes in increasing order of stash operations performed.

See stash contents

git stash list simply displays the list of all stashes on the system but it does not tell you what files are inside it.
It might happen that there are multiple stashes and you are looking for a stash with some particular changes before finally applying them.
For that you should be able to see the contents or the files inside a stash.

This can be done with show option of git stash command as shown below

git stash show

This will list all the files inside the topmost stash or the stash at highest index as below

$ git stash show
build.gradle | 1
+
1 file changed, 2 insertion(+)

Above stash contains 1 file which has 2 changes.

To view a particular stash, use its index as shown below

git stash show stash@{1}

You can also view untracked files with -u flag as

git stash show -u

Untracked files are those which have not been committed yet.
They are present on your local system only.
By default, git stash show will not include untracked files, even if they are part of the stash.

View contents of stash

Many times, listing only files of a stash are not enough. You also want to peek into the changes in each file inside a stash.
For this, use -p flag with show option of git stash command as below

git stash show -p

Supposing that there are 2 files in the most recent stash, below will be the output

diff –git a/Printer.java b/Printer.java
index abcdef1..1234567 100644
— a/Printer.java
+++ b/Printer.java
@@ -1,5 +1,5 @@
public class Printer {

– public void print(String message) {
+ public void printMessage(String message) {
System.out.println(“Printing: ” + message);
}
}
diff –git a/pom.xml b/pom.xml
index 9876543..abcdef2 100644
— a/pom.xml
+++ b/pom.xml
@@ -10,7 +10,7 @@
<groupId>com.example</groupId>

<artifactId>example-project</artifactId>
<version>1.0.0</version>
<artifactId>example-project</artifactId>

<version>1.0.0</version>
– </dependency>
+ </dependency>

– <build>
+ <build>
<!– Build configuration –>
<plugins>
<!– Plugin configurations –>

In this example:

  • For Printer.java, you can see the changes made to the print method.
    The - symbol indicates lines that were removed, and the + symbol indicates lines that were added.
  • For pom.xml, you can see changes in the <build> section.
    Similar to Printer.java, - indicates lines removed, and + indicates lines added.

This diff format allows you to see exactly what changes were made to each file in the stash.

Hope you learned how to see the contents of a git stash without actually applying it.