How to see stashed changes using git stash

The command git stash is used to stash the changes in a dirty working directory away.

You can list all stashed change using the command  git stash list,

$ git stash list
stash@{0}: 
stash@{1}:
stash@{2}:
stash@{3}:

Every time you stash your working directory, git will save the state of working directory into somethine which mantins history of stash tree. Every time you stash your changes it will be save as a kind of commit in stash tree. All stashed changes are stacked in the order with serial or reference. stash@{0} is the first or top most or recent stash. Every time if you want to refer a particular stash you have to use the it’s reference id something like stash@{3}, stash@{5}… etc

Think of each stash as a separate  commit. These commits are stored and stacked differently and not overlapped with conventional git commit history 

A stash is represented as a commit whose tree records the state of the working directory, and its first parent is the commit at HEAD when the stash was created.

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.

Command 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. Something like,

Something like below

git stash show
etc/project.conf | 6 +-----
1 file changed, 1 insertion(+), 5 deletions(-)

Some times this is not so useful. You may want to see the difference against  current HEAD or any specific commit or current directory.

If you use git stash show along with option -p, It will show all changes.

$ git stash show -p

 

Check diff against selected stash.

git stash show -p stash@{0}

Command 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. For more information check git-show

Synopsis: 

git show [options] <object>…


To see top most stash difference against  HEAD:

$ git show stash


To get diff of of selected stash against HEAD:

$ git show stash@{1}


See selected complete whole file as if stash is applied  from selected stash:

$ git show stash@{0}:<file_name>

Where,

stash@{0} is the reference of stash. I could be any one of stash@{0}, stash@{1}, stash@{2}… etc.

<file_name>  is the name of the file relative to project/git repository

Command 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.

$ git diff stash


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>

 

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

[1] https://git-scm.com/docs/git-show
[2] https://git-scm.com/docs/git-stash

 

2 thoughts on “How to see stashed changes using git stash

Leave a Reply

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