Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I diff the same file between two different commits on the same branch?

Tags:

git

git-diff

In Git, how could I compare the same file between two different commits (not contiguous) on the same branch (master for example)?

I'm searching for a compare feature like the one in Visual SourceSafe (VSS) or Team Foundation Server (TFS).
Is it possible in Git?

like image 507
systempuntoout Avatar asked Jul 26 '10 19:07

systempuntoout


People also ask

How do I compare files between two commits?

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 diff between two commits in GitHub?

You can also compare two arbitrary commits in your repository or its forks on GitHub in a two-dot diff comparison. To quickly compare two commits or Git Object IDs (OIDs) directly with each other in a two-dot diff comparison on GitHub, edit the URL of your repository's "Comparing changes" page.

How can you find out the differences between the files in two different branches?

In order to see the commit differences between two branches, use the “git log” command and specify the branches that you want to compare. Note that this command won't show you the actual file differences between the two branches but only the commits.


2 Answers

From the git-diff manpage:

git diff [--options] <commit> <commit> [--] [<path>...] 

For instance, to see the difference for a file "main.c" between now and two commits back, here are three equivalent commands:

$ git diff HEAD^^ HEAD main.c $ git diff HEAD^^..HEAD -- main.c $ git diff HEAD~2 HEAD -- main.c 
like image 184
mipadi Avatar answered Oct 09 '22 03:10

mipadi


You can also compare two different files in two different revisions, like this:

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

like image 36
Jakub Narębski Avatar answered Oct 09 '22 04:10

Jakub Narębski