Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git - easiest way to see diff with previous version if I have the sha

Tags:

git

git-diff

A colleague of mine checked in some changes to Git, and I want to see exactly what those changes were. In other words, the diff between his check-in and its parent.

What seemed logical to me was to run this command:

git diff shaOfHisCheckIn

But this didn't work. It appears to show the diff between that SHA and my current working copy.

What's the correct command to show the diff between a given SHA and its parent?

like image 594
Ryan Lundy Avatar asked May 25 '11 17:05

Ryan Lundy


People also ask

How do I diff two versions of a file in git?

You can run the git diff HEAD command to compare the both staged and unstaged changes with your last commit. You can also run the git diff <branch_name1> <branch_name2> command to compare the changes from the first branch with changes from the second branch.

How do I see my git diffs?

The diff can be done with git diff (followed by the filename or nothing if you want to see the diff of all modified files). But if you already did something like git add * , you have to undo with git restore --staged .

How can I see the difference after a commit?

To see the diff for a particular COMMIT hash, where COMMIT is the hash of the commit: git diff COMMIT~ COMMIT will show you the difference between that COMMIT 's ancestor and the COMMIT . See the man pages for git diff for details about the command and gitrevisions about the ~ notation and its friends.

Why git diff does not show changes?

Your file is already staged to be committed. You can show it's diff using the --cached option of git. To unstage it, just do what git status suggests in it's output ;) You can check The Git Index For more info.


4 Answers

git show is your friend:

git show shaOfHisCheckIn
like image 158
Simon Whitaker Avatar answered Oct 24 '22 02:10

Simon Whitaker


If you want to view the diff visually in kdiff3, meld, kompare, xxdiff, tkdiff, diffuse

git difftool --dir-diff shaOfHisCheckIn^!

git difftool --tool=meld --dir-diff shaOfHisCheckIn^!

git difftool -t meld -d shaOfHisCheckIn^!
like image 32
Denilson Sá Maia Avatar answered Oct 24 '22 01:10

Denilson Sá Maia


Try this:

git diff shaOfHisCheckIn^ shaOfHisCheckIn

or

git diff shaOfHisCheckIn{^,}
like image 30
Vinoth Gopi Avatar answered Oct 24 '22 01:10

Vinoth Gopi


git diff shaOfHisCheckIn shaOfHisCheckIn^

like image 23
Jonas Heidelberg Avatar answered Oct 24 '22 01:10

Jonas Heidelberg