git diff two files on same branch, same commit

Git

Git Problem Overview


sorry if this question exists, I surprisingly could not find it :/

How can I perform a git diff between two files within the same branch & same commit?

i.e. git diff fileA.php fileB.php

(ideally with the easy to read color coding that git offers....or similar to what the program BeyondCompare does!)

Git Solutions


Solution 1 - Git

If you want to use git diff on two arbitrary files you can use git diff --no-index <file_a> <file_b>. The two files do not need to be tracked in git for this to work.

Solution 2 - Git

You don't need git for that, just use diff fileA.php fileB.php (or vimdiff if you want side by side comparison)

Solution 3 - Git

If the files you want to compare are in your working copy, you can use simply diff as pointed by the others, however if the files are in some revision you can specify a revision for each file

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

as noted here: https://stackoverflow.com/a/3343506/1815446

In your case (specifying the same revision for both files):

git diff <revisionX>:fileA.php <revisionX>:fileB.php

Solution 4 - Git

To make regular gnu diff look more like git diff, I would recommend these parameters:

diff -burN file_or_dir1 file_or_dir2

(where -b ignore spaces, -u unified diff, -r recursive, -N treat missing files as /dev/null).

It works great, but still does not have colors and git-style auto-paging is virtually absent. If you want to fix both (I do), install colordiff and use it like this:

colordiff -burN file_or_dir1 file_or_dir2 | less -R

This gives output and interface that is very close to actual git diff.

Solution 5 - Git

If you want to diff two local files in the same directory just use diff.

diff fileA.php fileB.php

Solution 6 - Git

Just use regular diff.

diff -Nua fileA.php fileB.php

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questiond-_-bView Question on Stackoverflow
Solution 1 - Gitrzrgenesys187View Answer on Stackoverflow
Solution 2 - GitJaredMcAteerView Answer on Stackoverflow
Solution 3 - GitPau FracésView Answer on Stackoverflow
Solution 4 - GitmvpView Answer on Stackoverflow
Solution 5 - GitMatt LaceyView Answer on Stackoverflow
Solution 6 - GitPeter van der DoesView Answer on Stackoverflow