Is it possible to preview stash contents in git?

GitGit Stash

Git Problem Overview


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?

Git Solutions


Solution 1 - Git

git stash show will show you the files that changed in your most recent stash. You can add the -p option to show the diff.

git stash show -p

If the stash you are interested in is not the most recent one, then add the name of the stash to the end of the command:

git stash show -p stash@{2}

Solution 2 - Git

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}

Solution 3 - Git

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

Solution 4 - Git

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

Solution 5 - Git

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

Solution 6 - Git

The following command can be used to extract diff of stashed change against 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} -- <filename>

4. 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} <commit-hash>

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

Solution 7 - Git

Beyond the gitk recommendation in https://stackoverflow.com/questions/3573623/is-it-possible-to-preview-stash-application-in-git you can install tig and call tig stash. This free/open console program also allows you to choose which stash to compare

Solution 8 - Git

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

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

Solution 9 - Git

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.

Solution 10 - Git

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 https://stackoverflow.com/questions/10725729/git-see-whats-in-a-stash-without-applying-stash#comment27975633_10726185)

Solution 11 - Git

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

Solution 12 - Git

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

Solution 13 - Git

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^!"

Solution 14 - Git

You can review the stashed changes in VSCode with gitlen extension

screenshot of gitlen stashes

Solution 15 - Git

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.

Solution 16 - Git

Several answers have mentioned the -p (or --patch) flag on git stash show

However, it might also be worth mentioning that you can make this the default behaviour when showing a Git stash (ie. git stash show stash@{0}), either by using git config:

git config --global stash.showPatch true

...or in your .gitconfig file:

[stash]
  showPatch = true

This makes git stash show behave like git show by default. If and when you do just want to see the diffstat (ie. summary of lines added / deleted), you can still see that via the --stat flag (again, like with git show):

git stash show --stat stash@{0}

Solution 17 - Git

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenjolView Question on Stackoverflow
Solution 1 - GitJlewView Answer on Stackoverflow
Solution 2 - GitsegfaultView Answer on Stackoverflow
Solution 3 - GitJeff WardView Answer on Stackoverflow
Solution 4 - GitWesley MusgroveView Answer on Stackoverflow
Solution 5 - GithlongmoreView Answer on Stackoverflow
Solution 6 - GithxysayhiView Answer on Stackoverflow
Solution 7 - GitBruceView Answer on Stackoverflow
Solution 8 - GitWalterwhitesView Answer on Stackoverflow
Solution 9 - Gituser202729View Answer on Stackoverflow
Solution 10 - GitseanfView Answer on Stackoverflow
Solution 11 - GitBharat PahalwaniView Answer on Stackoverflow
Solution 12 - GitVishvajit PathakView Answer on Stackoverflow
Solution 13 - GitJBertView Answer on Stackoverflow
Solution 14 - GitWong DevrsionView Answer on Stackoverflow
Solution 15 - Gitakhil_mittalView Answer on Stackoverflow
Solution 16 - GitNick FView Answer on Stackoverflow
Solution 17 - GitccpizzaView Answer on Stackoverflow