View List of Stash Entries in Git

We are often required to pause on the work and focus on something else in a development environment. Thus, we may need to save our current work for the time being and focus on the different work. We later would like to resume back our original work.

The stash feature of Git, provided by the git stash command, allows us to save our changes for the work at hand temporarily. It will enable us to later resume our work by retrieving it from the temporary store.

We can do this operation of temporary saving of work many times. We thus would like to view the list of such stash entries and their contents. The git stash command provides us with options to browse the list of stash entries.

This tutorial will learn how to view the list of stash entries in Git. We will now illustrate this with an example.

Viewing List of Stash Entries in Git

The git stash command allows us to record the current state of the working directory of the project repository. It also allows us to save the current state of the index.

The git stash command saves local changes and reverts the working directory to match the HEAD commit. We can do this operation of shelving the changes on the working copy many times.

Thus, after performing the stashing many times, we now have a list of stash entries in our project’s Git repository. We can view the list of stash entries with the git stash list command.

The latest stash entry we created is stored in the refs/stash. The older stashes are found in the reflog of this reference. The most recent stash entry that is created is named stash@{0}. The one before it is named stash@{1} and so on.

The stashes can be referenced by specifying the stash index. For example, the integer n is equivalent to stash@{n}.

After creating a few stash entries, we can view them like this.

$ git stash list
stash@{0}: WIP on main: b14f387 some work
stash@{1}: WIP on main: b14f387 some other work
stash@{2}: WIP on main: b14f387 some older work

As shown above, we can see the list of three stash entries in the main branch of our Git repository. We can also view the contents of each stash entry.

To view the files in the most recent stash entry, we need to follow.

$ git stash show
 test.txt | 4 ++++
 1 file changed, 4 insertions(+)

We can see that the test.txt has been stashed most recently.

To view the changes of the files in the most recent stash entry, we need to do as follows.

$ git stash show -p
diff --git a/test.txt b/test.txt
index fae50f7..f60e878 100644
--- a/test.txt
+++ b/test.txt
@@ -73,4 +73,16 This test
 
 some old text
 
+ some new text
+1. Add this
+2. Add that
+

The git stash show command shows the changes recorded in the stash entry as a diff between the stashed contents and the commit back when the stash entry was first created.

We can also view a specific stash using a name. We need to provide the name in git stash show -p <named-stash>.

Thus, to view the stash entry with the name stash@{1}, we would execute the command as follows.

$ git stash show -p stash@{1}

We can also see the stash entries across branches, not just the current one. We need to execute the following command for that.

$ git stash list --all

We can also view the stash history in Git by a date range. We need to use the command git stash list with the option --before or --after.

We can execute the command with the date range as below.

$ git stash list --before 3.days.ago
$ git stash list --after 2.days.ago

We can use the –stat option to summarize each element’s changes in the git stash history. As shown below, we can execute the command with the -stat option.

$ git stash list --stat

Thus, we have shown how to view the list of stash entries in Git.

Write for us
DelftStack articles are written by software geeks like you. If you also would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Git Stash

  • Git Stash Specific Files
  • Delete Stash Record in Git