With git 1.9/2.0 (Q1 2014), you can use git merge-base --fork-point to ask for the best common ancestor according to Git.
You can see that new option:
- detailed in “How do I recover/resynchronise after someone pushes a rebase or a reset to a published branch?”.
- used in “How do you deal with a public repository that has already been rebased?”.
And since commit ad8261d from John Keeping (johnkeeping), git rebase can use that same new --fork-point option, which can come in handy should you need to rebase a branch like customers/acme_patches onto devel.
(I am not saying this would make sense in your specific scenario)
Note: Git 2.16 (Q1 2018) does clarify and enhance documentation for “merge-base --fork-point“, as it was clear what it computed but not why/what for.
See commit 6d1700b (09 Nov 2017) by Junio C Hamano (gitster).
(Merged by Junio C Hamano — gitster — in commit 022dd4a, 27 Nov 2017)
merge-base --fork-pointdoc: clarify the example and failure modesThe illustrated history used to explain the
--fork-pointmode
named three keypoint commits B3, B2 and B1 from the oldest to the
newest, which was hard to read.
Relabel them to B0, B1, B2.
Also illustrate the history after the rebase using the--fork-pointfacility was made.The text already mentions use of reflog, but the description is not
clear what benefit we are trying to gain by using reflog.
Clarify that it is to find the commits that were known to be at the tip of
the remote-tracking branch.
This in turn necessitates users to know the ramifications of the underlying assumptions, namely, expiry of reflog entries will make it impossible to determine which commits were at the tip of the remote-tracking branches and we fail when in doubt (instead of giving a random and incorrect result without even
warning).
Another limitation is that it won’t be useful if you did not fork from the tip of a remote-tracking branch but from in the middle.
Describe them.
So the documentation now reads:
After working on the
topicbranch created withgit checkout -b, the history of remote-tracking branch
topic origin/master
origin/mastermay have been rewound and rebuilt, leading to a
history of this shape:
o---B2
/
---o---o---B1--o---o---o---B (origin/master)
\
B0
\
D0---D1---D (topic)
where
origin/masterused to point at commits B0, B1, B2 and now it
points at B, and yourtopicbranch was started on top of it back
whenorigin/masterwas at B0, and you built three commits, D0, D1,
and D, on top of it.
Imagine that you now want to rebase the work you did on thetopicon top of the updatedorigin/master.In such a case,
git merge-base origin/master topicwould return the
parent of B0 in the above picture, butB0^..Dis not the range of
commits you would want to replay on top of B (it includes B0, which
is not what you wrote; it is a commit the other side discarded when
it moved its tip from B0 to B1).
git merge-base --fork-point origin/master topicis designed to help in such a case.
It takes not only B but also B0, B1, and B2 (i.e. old tips of the remote-tracking branches your repository’s reflog knows about) into account to see on which commit your topic branch was built and finds B0, allowing you to replay only the commits on your topic, excluding the commits the other side later
discarded.Hence
$ fork_point=$(git merge-base --fork-point origin/master topic)
will find B0, and
$ git rebase --onto origin/master $fork_point topic
will replay D0, D1 and D on top of B to create a new history of this
shape:
o---B2
/
---o---o---B1--o---o---o---B (origin/master)
\ \
B0 D0'--D1'--D' (topic - updated)
\
D0---D1---D (topic - old)
A caveat is that older reflog entries in your repository may be
expired bygit gc.
If B0 no longer appears in the reflog of the remote-tracking branchorigin/master, the--fork-pointmode obviously cannot find it and fails, avoiding to give a random and useless result (such as the parent of B0, like the same command without the--fork-pointoption gives).Also, the remote-tracking branch you use the
--fork-pointmode
with must be the one your topic forked from its tip.
If you forked from an older commit than the tip, this mode would not find the fork point (imagine in the above sample history B0 did not exist,
origin/masterstarted at B1, moved to B2 and then B, and you forked
your topic atorigin/master^whenorigin/masterwas B1; the shape of
the history would be the same as above, without B0, and the parent
of B1 is whatgit merge-base origin/master topiccorrectly finds,
but the--fork-pointmode will not, because it is not one of the
commits that used to be at the tip oforigin/master).