[git] Is it possible to preview stash contents in git?

I often put work away for later, then other stuff comes along, and a few weeks later, I want to inspect the stash, and find out what changes it would make if I applied it to working tree in its current state.

I know I can do a git diff on the stash, but this shows me all the differences between the working tree and the stash, whereas I'm just interested to know what the stash apply is going to change.

How can I do this?

This question is related to git git-stash

The answer is


I like how gitk can show you exactly what was untracked or sitting in the index, but by default it will show those stash "commits" in the middle of all your other commits on the current branch.

The trick is to run gitk as follows:

gitk "stash@{0}^!"

(The quoting is there to make it work in Powershell but this way it should still work in other shells as well.)

If you look up this syntax in the gitrevisions help page you'll find the following:

The r1^! notation includes commit r1 but excludes all of its parents. By itself, this notation denotes the single commit r1.

This will apparently put gitk in such a mode that only the immediate parents of the selected commit are shown, which is exactly what I like.


If you want to take this further and list all stashes then you can run this:

gitk `git stash list '--pretty=format:%gd^!'`

(Those single quotes inside the backticks are necessary to appease Bash, otherwise it complains about the exclamation mark)

If you are on Windows and using cmd or Powershell:

gitk "--argscmd=git stash list --pretty=format:%gd^!"

First we can make use of git stash list to get all stash items:

$git stash list
stash@{0}: WIP on ...
stash@{1}: WIP on ....
stash@{2}: WIP on ...

Then we can make use of git stash show stash@{N} to check the files under a specific stash N. If we fire it then we may get:

$ git stash show stash@{2}
fatal: ambiguous argument 'stash@2': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'

The reason for this may be that the shell is eating up curly braces and git sees stash@2 and not stash@{2}. And to fix this we need to make use of single quotes for braces as:

git stash show stash@'{2'}
com/java/myproject/my-xml-impl.xml                     | 16 ++++++++--------
com/java/myproject/MyJavaClass.java                    | 16 ++++++++--------
etc.

To view a current list of stash:

git stash list

You'll see a list like this:

stash@{0}: WIP on ...
stash@{1}: ...
stash@{2}: ...
...

To view diff on any of those stashes:

git stash show -p stash@{n}

Beyond the gitk recommendation in Is it possible to preview stash contents in git? you can install tig and call tig stash. This free/open console program also allows you to choose which stash to compare


Show all stashes

File names only:

for i in $(git stash list --format="%gd") ; do echo "======$i======"; git stash show $i; done

Full file contents in all stashes:

for i in $(git stash list --format="%gd") ; do echo "======$i======"; git stash show -p $i; done

You will get colorized diff output that you can page with space (forward) and b (backwards), and q to close the pager for the current stash. If you would rather have it in a file then append > stashes.diff to the command.


In additional to the existing answers which suggests using (to show the diff of the third-to-last stash)

git stash show -p stash@{2}

Note that in the git-stash documentation, it is written that

Stashes may also be referenced by specifying just the stash index (e.g. the integer n is equivalent to stash@{n}).

Therefore it's also possible to use (this is equivalent to the command above)

git stash show -p 2

Which should also avoid some Powershell issues.


You can review the stashed changes in VSCode with gitlen extension

screenshot of gitlen stashes


I'm a fan of gitk's graphical UI to visualize git repos. You can view the last item stashed with:

gitk stash

You can also use view any of your stashed changes (as listed by git stash list). For example:

gitk stash@{2}

In the below screenshot, you can see the stash as a commit in the upper-left, when and where it came from in commit history, the list of files modified on the bottom right, and the line-by-line diff in the lower-left. All while the stash is still tucked away.

gitk viewing a stash


The following command can be used to extract diff of stashed change againest any other stash or commit or branch or HEAD.

git stash show
git show
git diff
git difftool

Let’s see, how we can use each of the above mentioned commands.

  1. git stash show

The simple command git stash show gives very brief summary of changes of file, but will not show the diff of changes against current HEAD.

  1. git show

The command git-show is used to see various types of objects.

The command git-show is not only used to visualize stash changes, but also used to see one or more objects like blobs, trees, tags and commits.

  1. git diff

The command git-diff is also one of common command which is used to show changes between commits, commit and working tree, etc.

By default, git diff will show the diff of selected stash against(modified files) current state of repository unless other stash reference or commit is specified.

To get difference between top most stash stash@{0} and master branch:

$ git diff stash@{0} master

Only display the names of file not diff of changes:

$ git diff --name-only stash@{0} master

See the diff between selected stashes for a selected file:

$ git diff stash@{0}^1 stash@{0} --

  1. git difftool

The command git-difftool can also be used to find diff between selected stash and selected commit or branch or stash

See the difference between latest two stashes:

$ git difftool stash@{0} stash@{0}^1

git difftool --dir-diff stash@{0} stash@{0}^1

Summary:

Commands which are useful to extract the diff from selected stash git stash show, git show, git diff, git difftool .

See difference using command git stash show,

git stash show -p stash@{0}

See the changes in the stash using command git show,

git show stash@{1}

See the difference between latest stash and selected commit using command git diff,

git diff stash@{0}

References:

https://howto.lintel.in/how-to-see-stashed-changes-using-git-stash/

https://git-scm.com/docs/git-show

https://git-scm.com/docs/git-stash


You can view all stashes' list by the following command:

$ git stash list

stash@{0}: WIP on dev: ddd4d75 spelling fix

stash@{1}: WIP on dev: 40e65a8 setting width for messages

......

......

......


stash@{12}: WIP on dev: 264fdab added token based auth

Newest stash is the first one.

You can simply select index n of stash provided in the above list and use the following command to view stashed details

git stash show -p stash@{3}

Similarly,

git stash show -p stash@{n}

You can also check diff by using the command :

git diff HEAD stash@{n} -- /path/to/file

When this question was first asked, this may not have been an option, but, if you use PyCharm, you can use the UnStash Changes tool (VCS->Git->UnStash Changes...). This allows you to view the list of stashed changes, as well as pop, drop, clear, or apply (into a new branch if desired):

Unstash Changes Window

and view the changed files per stash:

Paths Affected Window

as well as diffs per file. In the diffs you can cherry-pick individual changes to apply from the stashed changes to the working branch (using the left-pointing chevron):

enter image description here


To view all the changes in an un-popped stash:

git stash show -p stash@{0}

To view the changes of one particular file in an un-popped stash:

git diff HEAD stash@{0} -- path/to/filename.php

I use this to see all my stashes with colour diff highlighting (on Fedora 21):

git stash list | 
  awk -F: '{ print "\n\n\n\n"; print $0; print "\n\n"; 
  system("git -c color.ui=always stash show -p " $1); }' | 
  less -R

(Adapted from Git: see what's in a stash without applying stash)


View list of stashed changes

git stash list

For viewing list of files changed in a particular stash

git stash show -p stash@{0} --name-only

For viewing a particular file in stash

git show stash@{0} path/to/file

yes the best way to see what is modified is to save in file like that:

git stash show -p stash@{0} > stash.txt