Introduction
A Git stash allows you to hide unfinished work from the current working directory and return to it later. There is no limit to the number of Git stashes you can create, which means the stash reference can hold multiple entries.
In this tutorial, you will learn to view Git stash entries.
Prerequisites
- Git installed (follow our tutorials for installing Git on Ubuntu, macOS, Windows, CentOS 7, or CentOS 8).
- A Git repository.
What is Git Stash List?
Creating a stash using the git stash
command sets aside the uncommitted changes and reverts the working directory to match the HEAD commit. Git allows users to create as many stashes as needed.
When creating a stash using the git stash
command, the changes are saved in the refs/stash reference. Each stash entry is assigned an index number, where the latest entry is stash@{0}
, then comes stash@{1}
, and so on.
A Git stash list shows the existing stash entries, the name of the branch on which the entry was created, and a description of the commit the entry is based on.
How to Use Git Stash List?
Running the git stash list
command outputs a list of all the stashes you created. Customize the output by specifying options applicable to the git log command. The options provide many enhancements and formatting options.
The sections below showcase different examples of using the git stash list
command and the available options.
View the Git Stash History
See the Git stash history by running the default list
command without options. The command outputs the entire stash history.
Run the following command:
git stash list
The most recent stash has the index {0}
. Additionally, if specified during creation, the output contains a message for each stash. If there is no stash message, the output shows the message related to the commit the stash is based on.
See Stashes Attached to All Branches
Use the --all
option to display stashes attached to all branches instead of only showing the currently active one. Run the following command:
git stash list --all
The partial output above shows stashes from all Git branches in the repository.
Show Limited Number of Stashes
If you have a long git stash history with many stash entries, specify a limited number of the most recent stashes to show in the output.
The syntax is:
git stash list -[n]
- For
[n]
, specify the number of stashes to show in the output.
For example, to show the two most recent stash entries, run:
git stash list -2
View Stashes By Date
Use the --after
option followed by date to show only the stashes more recent than the specified date. Alternatively, use the --before
option to show only the stashes older than the specified date.
The accepted date formats are:
- Mon, 5 Jul 2022 17:18:43 +0200
- 2022-07-05 17:18:43 +0200
- Mon Jul 5 15:18:43 2022
- 2022-07-05
- 5.hours.ago
- 3.years.2.months.ago
- 6am yesterday
For example, to show only the stashes older than five days, enter:
git stash list --before 5.days.ago
The output contains only one stash, which was created more than five days ago.
See Stash Contents
After listing the stash history, see the contents of a specific stash using the show
command. The syntax is:
git stash show stash@{n}
- For
{n}
, specify the stash index whose contents you want to see.
For example, to view the contents of stash{0}
, run:
git stash show stash@{0}
The output shows the stashed files, along with the number of file insertions and deletions.
Note: Untracked files are excluded from Git stashes unless you specifically instruct to include them. See how to stash untracked files and then view the stash contents.
View the Changes in Git Stash Entries
Specify the -p
option to view the diff of changes for each stash. Run the following command:
git stash list -p
The partial output above shows the diffs for stash{0}
. To see other stash diffs, scroll through the terminal window.
Alternatively, to see the difference between a stash and the local Git working tree, specify the stash you want to compare.
The syntax is:
git stash show -p stash@{n}
See Stats for Stashed Files
Specify the --stat
option to output a summary of changes for each file in the Git stash history. Run the following command:
git stash list --stat
The command outputs the number and summary of changes for the files in each stash.
Show Detailed Stash List
For a concise Git stash list with more details about each stash, specify the --oneline
option with the list
command. Enter the following:
git stash list --oneline
The command outputs a list of stashes, the stash commit SHA, the stash paths, and a message created for the stash, or commit message the stash is based on.
Conclusion
This tutorial showed how to list Git stash entries in a repository and utilize different command options to format and customize the output.
Learn more about Git in our tutorials for restoring a Git stash, dropping a Git stash, or stashing specific files.