Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git diff two files on same branch, same commit

Tags:

git

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

like image 226
d-_-b Avatar asked Dec 20 '12 02:12

d-_-b


People also ask

Can you git diff two files?

The git diff command displays the differences between files in two commits or between a commit and your current repository. You can see what text has been added to, removed from, and changed in a file. By default, the git diff command displays any uncommitted changes to your repository.

How do I compare files between branches?

Compare specific file between two branches In some cases, you may want to see all changes done to a specific file on the current branch you are working on. In order to see the differences done to a file between two branches, use the “git diff” command, specify the two branches and the filename.

Does git diff use diff?

Comparing changes with git diffgit diff is a multi-use Git command that when executed runs a diff function on Git data sources. These data sources can be commits, branches, files and more.


4 Answers

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.

like image 53
rzrgenesys187 Avatar answered Oct 23 '22 16:10

rzrgenesys187


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

like image 36
JaredMcAteer Avatar answered Oct 23 '22 16:10

JaredMcAteer


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
like image 25
Pau Fracés Avatar answered Oct 23 '22 16:10

Pau Fracés


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.

like image 24
mvp Avatar answered Oct 23 '22 16:10

mvp