I have looked at various SO answers on using git diff
and git revisions (HEAD, ORIG_HEAD, FETCH_HEAD, etc.) and I still haven\'t found an easy way to list the c
You can find the branch point using git merge-base
. Consider master
the mainline and dev
the branch whose history you are interested in. To find the point at which dev
was branched from master
, run:
git merge-base --fork-point master dev
We can now diff dev
against this basis:
git diff $(git merge-base --fork-point master dev)..dev
If dev
is the current branch this simplifies to:
git diff $(git merge-base --fork-point master)
For more information see the git-merge-base documentation.
The current solution mentions
Use
git diff @{u}...HEAD
, with three dots.
But... this is best done with Git 2.28 (Q3 2020).
Before, "git diff
" used to take arguments in random and nonsense range notation, e.g. "git diff A..B C ", "git diff A..B C...D", etc., which has been cleaned up.
See commit b7e10b2, commit 8bfcb3a (12 Jun 2020), and commit bafa2d7 (09 Jun 2020) by Chris Torek (chris3torek).
(Merged by Junio C Hamano -- gitster -- in commit 1457886, 25 Jun 2020)
git diff: improve range handling
Signed-off-by: Chris Torek
When git diff is given a symmetric difference
A...B
, it chooses some merge base from the two specified commits (as documented).This fails, however, if there is no merge base: instead, you see the differences between A and B, which is certainly not what is expected.
Moreover, if additional revisions are specified on the command line ("git diff A...B C"), the results get a bit weird:
If there is a symmetric difference merge base, this is used as the left side of the diff.
The last final ref is used as the right side.If there is no merge base, the symmetric status is completely lost.
We will produce a combined diff instead.Similar weirdness occurs if you use, e.g., "git diff C A...B D". Likewise, using multiple two-dot ranges, or tossing extra revision specifiers into the command line with two-dot ranges, or mixing two and three dot ranges, all produce nonsense.
To avoid all this, add a routine to catch the range cases and verify that that the arguments make sense.
As a side effect, produce a warning showing which merge base is being used when there are multiple choices; die if there is no merge base.
The documentation now includes:
'git diff' [<options>] <commit> [<commit>...] <commit> [--] [<path>...]
:This form is to view the results of a merge commit.
The first listed must be the merge itself; the remaining two or more commits should be its parents.
A convenient way to produce the desired set of revisions is to use the{caret}@
suffix.
For instance, ifmaster
names a merge commit,git diff master master^@
gives the same combined diff asgit show master
.
Use git diff @{u}...HEAD
, with three dots.
With two dots, or with HEAD
omitted, it will show diffs from changes on both sides.
With three dots, it will only show diffs from changes on your side.
Edit: for people with slightly different needs, you might be interested in git merge-base
(note that it has plenty more options than the other answer uses).
To diff against the remote master branch:
git diff $(git merge-base HEAD origin/master)..
For diffs, you want the three-dot notation. If your branch is called dev
and it branched from master
:
% git diff master...dev
For log, you want the two-dot notation:
% git log master..dev
The revision syntax r1..r2
(with two dots) means "everything reachable from r2
(inclusive) but not reachable from r1
(inclusive)". The normal way to use this is to think of r1
and r2
as specifying a range in a sequence of commits (r1
exclusive, r2
inclusive), so if you have 10 revisions, 3..7
will show you changes 4, 5, 6, and 7. It's {1, 2, 3, 4, 5, 6, 7}
minus {1, 2, 3}
. But r1
doesn't necessarily have to be an ancestor of r2
. Think of it more like a set operation where r1
represents the entire ancestry from r1
backwards, and r2
represents the entire ancestry from r2
backwards, and you're subtracting the first set from the second set.
So then:
git log master..dev
is the entire history of the branch minus the entire history of master. In other words, just the branch.
You can diff the current branch from the branch start point using:
git diff (start point)...
Where (start point) is a branch name, a commit-id, or a tag.
Eg if you're working on a feature branch branched from develop
, you can use:
git diff develop...
for all changes on the current branch since the branch point.
This was already mentioned in a comment, but I think it deserves answer status. I don't know what it will do since last rebase.