123

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

d-_-b
  • 21,536
  • 40
  • 150
  • 256
  • Note: [Git: How to diff two different files in different commits?](https://stackoverflow.com/questions/43041882/git-how-to-diff-two-different-files-in-different-commits) is slightly more general than this question, but the answer is essentially the same as [the correct answer to this one](https://stackoverflow.com/a/13964556/294313), and it doesn't have all of those bogus answers, so I've voted to close this as a dupe of it. – SamB May 26 '19 at 22:57

6 Answers6

294

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.

rzrgenesys187
  • 3,246
  • 2
  • 17
  • 7
  • 38
    +1 as it answers the inital question (regardless of "why would you do that"... there can be reasons) – NobodysNightmare Mar 25 '14 at 12:08
  • 22
    `--word-diff` highlights changes by word, not just line – user3148949 Oct 27 '15 at 09:46
  • 2
    For those of us using PowerShell, this is a far easier way to get a diff of two files in a repo. – Ifrit Jan 23 '18 at 14:44
  • 3
    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! – j_random_hacker Nov 10 '18 at 21:52
  • @daboross In GNU diffutils 3.4+ (2016-08-08), `diff` has `--color`, which might help (see https://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 https://stackoverflow.com/a/4857407/10095231) does not work. – kelvin Nov 04 '19 at 21:37
  • A good reason is you're on Windows with git installed and no diff – Jason Nov 02 '20 at 22:24
91

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

JaredMcAteer
  • 21,688
  • 5
  • 49
  • 65
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
Community
  • 1
  • 1
Pau Fracés
  • 1,077
  • 10
  • 22
  • 2
    Note: A [revision](https://git-scm.com/book/en/v2/Git-Tools-Revision-Selection#_revision_selection) can be `HEAD~2`, a SHA-1, a branch name, etc. – ma11hew28 Nov 17 '16 at 13:06
  • 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. – Frank Forte Feb 13 '18 at 18:41
  • 1
    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?" – TrentP Apr 19 '18 at 06:23
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.

meetar
  • 7,443
  • 8
  • 42
  • 73
mvp
  • 111,019
  • 13
  • 122
  • 148
0

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

diff fileA.php fileB.php
int3
  • 12,861
  • 8
  • 51
  • 80
Matt Lacey
  • 8,227
  • 35
  • 58
-4

Just use regular diff.

diff -Nua fileA.php fileB.php
Peter van der Does
  • 14,018
  • 4
  • 38
  • 42