130

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!)

1

6 Answers 6

304

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.

Sign up to request clarification or add additional context in comments.

6 Comments

+1 as it answers the inital question (regardless of "why would you do that"... there can be reasons)
--word-diff highlights changes by word, not just line
For those of us using PowerShell, this is a far easier way to get a diff of two files in a repo.
How is this excellent answer listed below 2 earlier, fewer-vote-having (and less useful) answers when sorting by "active"??? I'm not even counting the answer marked as correct. Anyway, thanks!
@daboross In GNU diffutils 3.4+ (2016-08-08), diff has --color, which might help (see unix.stackexchange.com/a/338960). But even then, if using diff-so-fancy, it seems to require the specific output format of git diff. diff -u (see stackoverflow.com/a/4857407/10095231) does not work.
|
95

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

7 Comments

vimdiff for the win! Thanks!
This may work, but it isn't git diff. It's a completely different output format.
This doesn't answer the original question
I disagree, the OP wasn't clear but the fact that he mentions BeyondCompare implies that he's not specifically looking for a git diff but simply an easy to read diff. Also the fact he accepted my answer as what he was looking for also indicates that it does in fact answer it.
read the answer closely. the command is diff fileA fileB not git diff fileA fileB
|
18

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 

3 Comments

Note: A revision can be HEAD~2, a SHA-1, a branch name, etc.
This worked for me, just comparing file names from two different directories: git diff --name-only HEAD:dir1 HEAD:dir1_backup works after a git add of both directories.
This works if the revision is not checked out into the working tree, or the working tree files are modified and one wishes to diff the originals, or if there is no working tree at all, as in a bare git repo. It is unfortunate that this highly rated question has highly rated answers that answer a still useful, yet quite different, question, "how can I get git-diff like output without using git?"
6

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.

4 Comments

-1 because "you can't" is incorrect (see: stackoverflow.com/a/13965200/968531 )
Remove the "you can't", add instructions for installing colordiff and this could be an OK answer to the original question.
Upvoted for suggesting colordiff, I learnt about it recently but always forget to use it.
this has to do with the version comparison of the same file therefore comparison between two different files is incorrect in this conext
0

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

diff fileA.php fileB.php 

2 Comments

This doesn't answer the original question
-4

Just use regular diff.

diff -Nua fileA.php fileB.php 

1 Comment

-1 because "you can't" is incorrect (see: stackoverflow.com/a/13965200/968531 )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.