How do I diff the same file between two different commits on the same branch? How do I diff the same file between two different commits on the same branch? git git

How do I diff the same file between two different commits on the same branch?


From the git-diff manpage:

git diff [--options] <commit> <commit> [--] [<path>...]

For instance, to see the difference for a file "main.c" between now and two commits back, here are three equivalent commands:

$ git diff HEAD^^ HEAD main.c$ git diff HEAD^^..HEAD -- main.c$ git diff HEAD~2 HEAD -- main.c


You can also compare two different files in two different revisions, like this:

git diff <revision_1>:<file_1> <revision_2>:<file_2>


If you have configured the "difftool" you can use

git difftool revision_1:file_1 revision_2:file_2

Example: Comparing a file from its last commit to its previous commit on the same branch:Assuming that if you are in your project root folder

$git difftool HEAD:src/main/java/com.xyz.test/MyApp.java HEAD^:src/main/java/com.xyz.test/MyApp.java

You should have the following entries in your ~/.gitconfig or in project/.git/config file. Install the p4merge [This is my preferred diff and merge tool]

[merge]    tool = p4merge    keepBackup = false[diff]    tool = p4merge    keepBackup = false[difftool "p4merge"]    path = C:/Program Files (x86)/Perforce/p4merge.exe[mergetool]    keepBackup = false[difftool]    keepBackup = false[mergetool "p4merge"]    path = C:/Program Files (x86)/Perforce/p4merge.exe    cmd = p4merge.exe \"$BASE\" \"$LOCAL\" \"$REMOTE\" \"$MERGED\"