Skip to content
Browse files

merge-base: teach "--fork-point" mode

The "git pull --rebase" command computes the fork point of the
branch being rebased using the reflog entries of the "base" branch
(typically a remote-tracking branch) the branch's work was based on,
in order to cope with the case in which the "base" branch has been
rewound and rebuilt.  For example, if the history looked like this:


where the current tip of the "base" branch is at Base, but earlier
fetch observed that its tip used to be B3 and then B2 and then B1
before getting to the current commit, and the branch being rebased
on top of the latest "base" is based on commit B3, it tries to find
B3 by going through the output of "git rev-list --reflog base" (i.e.
Base, B1, B2, B3) until it finds a commit that is an ancestor of the
current tip "Derived".

Internally, we have get_merge_bases_many() that can compute this
with one-go.  We would want a merge-base between Derived and a
fictitious merge commit that would result by merging all the
historical tips of "base".  When such a commit exist, we should get
a single result, which exactly match one of the reflog entries of

Teach "git merge-base" a new mode, "--fork-point", to compute
exactly that.

Helped-by: Martin von Zweigbergk <>
Helped-by: John Keeping <>
Signed-off-by: Junio C Hamano <>
  • Loading branch information
gitster committed Oct 23, 2013
1 parent 16e57ae commit d96855ff517560650c62eca51a8bb78263f6a3ef
Showing with 173 additions and 2 deletions.
  1. +36 −2 Documentation/git-merge-base.txt
  2. +109 −0 builtin/merge-base.c
  3. +28 −0 t/
@@ -13,6 +13,7 @@ SYNOPSIS
'git merge-base' [-a|--all] --octopus <commit>...
'git merge-base' --is-ancestor <commit> <commit>
'git merge-base' --independent <commit>...
'git merge-base' --fork-point <ref> [<commit>]

@@ -24,8 +25,8 @@ that does not have any better common ancestor is a 'best common
ancestor', i.e. a 'merge base'. Note that there can be more than one
merge base for a pair of commits.


As the most common special case, specifying only two commits on the
command line means computing the merge base between the given two commits.
@@ -56,6 +57,14 @@ from linkgit:git-show-branch[1] when used with the `--merge-base` option.
and exit with status 0 if true, or with status 1 if not.
Errors are signaled by a non-zero status that is not 1.

Find the point at which a branch (or any history that leads
to <commit>) forked from another branch (or any reference)
<ref>. This does not just look for the common ancestor of
the two commits, but also takes into account the reflog of
<ref> to see if the history leading to <commit> forked from
an earlier incarnation of the branch <ref> (see discussion
on this mode below).

@@ -137,6 +146,31 @@ In modern git, you can say this in a more direct way:


Discussion on fork-point mode

After working on the `topic` branch created with `git checkout -b
topic origin/master`, the history of remote-tracking branch
`origin/master` may have been rewound and rebuilt, leading to a
history of this shape:

---o---o---B2--o---o---o---B (origin/master)
Derived (topic)

where `origin/master` used to point at commits B3, B2, B1 and now it
points at B, and your `topic` branch was started on top of it back
when `origin/master` was at B3. This mode uses the reflog of
`origin/master` to find B3 as the fork point, so that the `topic`
can be rebased on top of the updated `origin/master` by:

$ fork_point=$(git merge-base --fork-point origin/master topic)
$ git rebase --onto origin/master $fork_point topic

See also
@@ -1,6 +1,9 @@
#include "builtin.h"
#include "cache.h"
#include "commit.h"
#include "refs.h"
#include "diff.h"
#include "revision.h"
#include "parse-options.h"

static int show_merge_base(struct commit **rev, int rev_nr, int show_all)
@@ -27,6 +30,7 @@ static const char * const merge_base_usage[] = {
N_("git merge-base [-a|--all] --octopus <commit>..."),
N_("git merge-base --independent <commit>..."),
N_("git merge-base --is-ancestor <commit> <commit>"),
N_("git merge-base --fork-point <ref> [<commit>]"),

@@ -85,6 +89,103 @@ static int handle_is_ancestor(int argc, const char **argv)
return 1;

struct rev_collect {
struct commit **commit;
int nr;
int alloc;
unsigned int initial : 1;

static void add_one_commit(unsigned char *sha1, struct rev_collect *revs)
struct commit *commit;

if (is_null_sha1(sha1))

commit = lookup_commit(sha1);
if (!commit ||
(commit->object.flags & TMP_MARK) ||

ALLOC_GROW(revs->commit, revs->nr + 1, revs->alloc);
revs->commit[revs->nr++] = commit;
commit->object.flags |= TMP_MARK;

static int collect_one_reflog_ent(unsigned char *osha1, unsigned char *nsha1,
const char *ident, unsigned long timestamp,
int tz, const char *message, void *cbdata)
struct rev_collect *revs = cbdata;

if (revs->initial) {
revs->initial = 0;
add_one_commit(osha1, revs);
add_one_commit(nsha1, revs);
return 0;

static int handle_fork_point(int argc, const char **argv)
unsigned char sha1[20];
char *refname;
const char *commitname;
struct rev_collect revs;
struct commit *derived;
struct commit_list *bases;
int i, ret = 0;

switch (dwim_ref(argv[0], strlen(argv[0]), sha1, &refname)) {
case 0:
die("No such ref: '%s'", argv[0]);
case 1:
break; /* good */
die("Ambiguous refname: '%s'", argv[0]);

commitname = (argc == 2) ? argv[1] : "HEAD";
if (get_sha1(commitname, sha1))
die("Not a valid object name: '%s'", commitname);

derived = lookup_commit_reference(sha1);
memset(&revs, 0, sizeof(revs));
revs.initial = 1;
for_each_reflog_ent(refname, collect_one_reflog_ent, &revs);

for (i = 0; i <; i++)
revs.commit[i]->object.flags &= ~TMP_MARK;

bases = get_merge_bases_many(derived,, revs.commit, 0);

* There should be one and only one merge base, when we found
* a common ancestor among reflog entries.
if (!bases || bases->next) {
ret = 1;
goto cleanup_return;

/* And the found one must be one of the reflog entries */
for (i = 0; i <; i++)
if (&bases->item->object == &revs.commit[i]->object)
break; /* found */
if ( <= i) {
ret = 1; /* not found */
goto cleanup_return;

printf("%s\n", sha1_to_hex(bases->item->object.sha1));

return ret;

int cmd_merge_base(int argc, const char **argv, const char *prefix)
struct commit **rev;
@@ -100,6 +201,8 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
N_("list revs not reachable from others"), 'r'),
OPT_CMDMODE(0, "is-ancestor", &cmdmode,
N_("is the first one ancestor of the other?"), 'a'),
OPT_CMDMODE(0, "fork-point", &cmdmode,
N_("find where <commit> forked from reflog of <ref>"), 'f'),

@@ -120,6 +223,12 @@ int cmd_merge_base(int argc, const char **argv, const char *prefix)
if (cmdmode == 'r' || cmdmode == 'o')
return handle_octopus(argc, argv, cmdmode == 'r', show_all);

if (cmdmode == 'f') {
if (argc < 1 || 2 < argc)
usage_with_options(merge_base_usage, options);
return handle_fork_point(argc, argv);

if (argc < 2)
usage_with_options(merge_base_usage, options);

@@ -230,4 +230,32 @@ test_expect_success 'criss-cross merge-base for octopus-step' '
test_cmp expected.sorted actual.sorted

test_expect_success 'using reflog to find the fork point' '
git reset --hard &&
git checkout -b base $E &&
for count in 1 2 3
git commit --allow-empty -m "Base commit #$count" &&
git rev-parse HEAD >expect$count &&
git checkout -B derived &&
git commit --allow-empty -m "Derived #$count" &&
git rev-parse HEAD >derived$count &&
git checkout -B base $E || exit 1
for count in 1 2 3
git merge-base --fork-point base $(cat derived$count) >actual &&
test_cmp expect$count actual || exit 1
) &&
# check that we correctly default to HEAD
git checkout derived &&
git merge-base --fork-point base >actual &&
test_cmp expect3 actual


0 comments on commit d96855f

Please sign in to comment.
You can’t perform that action at this time.