Understanding Git Stashes
Git stashes allow you to temporarily shelve (or stash) changes you’ve made to your working directory so you can work on something else, and then re-apply them later. This is incredibly useful when you need to switch branches quickly, fix a bug, or pull in updates without committing unfinished work. However, before applying a stash, it’s often desirable to inspect its contents to remember what changes it holds. This tutorial will guide you through the ways to view the contents of a stash without actually applying those changes to your working directory.
Listing Your Stashes
The first step is often to see which stashes you have saved. Git keeps track of stashes in a stack-like structure. You can list all your stashes using the git stash list
command:
git stash list
This will output a list of your stashes, typically formatted like this:
stash@{0}: WIP on main: a1b2c3d Initial commit
stash@{1}: WIP on feature/new-stuff: e4f5g6h Added some functionality
Each line represents a stash. stash@{0}
is the most recent stash, stash@{1}
is the second most recent, and so on. The "WIP" part indicates "Work in Progress," and the text following describes the branch you were on and a summary of the commit when you created the stash.
Viewing Stash Summaries
The git stash list
command provides a brief summary. For a more detailed view of the files contained within a specific stash, you can use git stash show
.
By default, git stash show
displays a summary of the changes in the most recent stash (stash@{0}):
git stash show
This will show a list of the files that were modified or added in the stash, along with indicators of how many lines were added or removed.
To view the files within a specific stash, provide the stash identifier (e.g., stash@{1}
):
git stash show stash@{1}
Viewing Detailed Changes with Patch Format
Often, you’ll want to see the actual diff of the changes contained within a stash. This lets you examine exactly what modifications were made. To do this, use the -p
(or --patch
) option with git stash show
.
To view the detailed changes in the most recent stash:
git stash show -p
To view the detailed changes in a specific stash:
git stash show -p stash@{1}
Or, a more concise equivalent:
git stash show -p 1
This command will output a standard patch-style diff, showing the lines that were added and removed. This is the most comprehensive way to inspect the contents of a stash without applying it.
You can also view the changes made by only the last stash using this command:
git stash show -p 0
Key Takeaways
git stash list
: Lists all your stashes.git stash show
: Shows a summary of files changed in the most recent stash.git stash show stash@{N}
: Shows a summary of files changed in the stash at index N.git stash show -p
: Displays the detailed changes (diff) in patch format for the most recent stash.git stash show -p stash@{N}
: Displays the detailed changes in patch format for the stash at index N.