ChatGPT解决这个技术问题 Extra ChatGPT

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?

colorized diff output: git stash show -p stash@{1} >~/.diff && vim ~/.diff (doesn't have to be vim. any text editor as long as your text editor has syntax highlighting support for diff output).
Note if you use Visual Studio, you can actually just double-click the stash entry from the Git Changes window to preview the changes that stash pop would make

W
Wayne Conrad

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}

Use git stash show -p stash@{0} to see a specific stash. 0 shows the last tone, 1 the second last one.. etc. git stash list will show all the available.
If you are using PowerShell, you will have to put the stash name in quotes. (ie: git stash show -p 'stash@{0}')
If you want to save diff in file use git stash show -p stash@{0}> stash.txt
In 2019 you can just type git stash show [-p] [stash index number]
m
mfaani

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}

J
Jeff Ward

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.

https://i.stack.imgur.com/XyLyw.jpg


You can provide multiple stash@{X} values on the command-line to see more results at once, but I haven't found a simple way to just say 'show all stash entries' in gitk.
gitk stash seems be shorthand for gitk stash@{0}
to show all stashes in gitk you can use gitk `git stash list --pretty=format:%gd` and then search for "WIP on" to jump to next stash.
gitk --reflog lets you see all the stashes, and more.
gitk was the only tool that showed me directly that the stash had untracked files saved in it. All the others just showed me "no differences".
V
Vadim Kotov

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

in case one does not remember the filenames it also works for all files changed git diff HEAD stash@{0}
Just for clarity: -p is short for --patch. The option comes from git-diff. If you prefer the long form you can write git stash show --patch.
A
Anentropic

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.

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.

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.

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>

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


h
hlongmore

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):

https://i.stack.imgur.com/Xm3ls.png

and view the changed files per stash:

https://i.stack.imgur.com/AqefW.png

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):

https://i.stack.imgur.com/r8ATv.png


thank you! I use PhpStorm or Intellij, I didn't know about this feature.
C
Community

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


Looks like a perfect tool for managing multiple stashes! You can also pop and drop stashes with P and ! keys respectively.
TUI alternatives are always good, but for the folk already used to or somehow preferring gitk it is relatively easy to hack it to show all the stashes.
W
Walterwhites

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

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

u
user202729

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.


C
Community

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)


B
Bharat Pahalwani

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

V
Vadim Kotov

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

J
JBert

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

W
Wong Devrsion

You can review the stashed changes in VSCode with gitlen extension

screenshot of gitlen stashes


Useful to know. Though, they were specifically asking about in git, not within a specific product.
a
akhil_mittal

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.

N
Nick F

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}

c
ccpizza

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.