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!)
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.
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.
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.
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.
You don't need git for that, just use diff fileA.php fileB.php
(or vimdiff if you want side by side comparison)
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
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With