Check stash content before applying in GIT – Code Example

Total
0
Shares

I want to inspect a 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 do I see what is inside a stash without actually applying it?

Solutions

From man git-stash (which can also be obtained via git help stash):

The modifications stashed away by this command can be listed with git stash list, inspected with git stash show, and …

show [<stash>]
    Show the changes recorded in the stash as a diff between the stashed
    state and its original parent. When no <stash> is given, shows the
    latest one. By default, the command shows the diffstat, but it will
    accept any format known to git diff (e.g., git stash show -p [email protected]{1}
    to view the second most recent stash in patch form).

Note: the -p option generates a patch, as per git-diff documentation.

List the stashes:

git stash list

Show the files in the most recent stash:

git stash show

Show the changes of the most recent stash:

git stash show -p

Show the changes of the named stash:

git stash show -p [email protected]{1}

Or in short:

git stash show -p 1 

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 [email protected]{2}

To view a current list of stash:

git stash list

You’ll see a list like this:

[email protected]{0}: WIP on ...
[email protected]{1}: ...
[email protected]{2}: ...
...

To view diff on any of those stashes:

git stash show -p [email protected]{n}

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 [email protected]{0} and master branch:

git diff [email protected]{0} master

Only display the names of file not diff of changes:

git diff --name-only [email protected]{0} master

See the diff between selected stashes for a selected file:

git diff [email protected]{0}^1 [email protected]{0} -- <filename>
  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 [email protected]{0} [email protected]{0}^1

git difftool --dir-diff [email protected]{0} [email protected]{0}^1

Summary:

Commands which are useful to extract the diff from selected stash: git stash showgit showgit diffgit difftool.

See difference using command git stash show,

git stash show -p [email protected]{0}

See the changes in the stash using command git show,

git show [email protected]{1}

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

git diff [email protected]{0} <commit-hash>

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

git stash show -p [email protected]{0}

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

git diff HEAD [email protected]{0} -- path/to/filename.php

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

git stash show -p [email protected]{0} > stash.txt

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

Conclusion

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.

Leave a Reply

Your email address will not be published. Required fields are marked *