1GIT-MERGE-BASE(1)                 Git Manual                 GIT-MERGE-BASE(1)
2
3
4

NAME

6       git-merge-base - Find as good common ancestors as possible for a merge
7

SYNOPSIS

9       git merge-base [-a|--all] <commit> <commit>...
10       git merge-base [-a|--all] --octopus <commit>...
11       git merge-base --is-ancestor <commit> <commit>
12       git merge-base --independent <commit>...
13       git merge-base --fork-point <ref> [<commit>]
14
15

DESCRIPTION

17       git merge-base finds best common ancestor(s) between two commits to use
18       in a three-way merge. One common ancestor is better than another common
19       ancestor if the latter is an ancestor of the former. A common ancestor
20       that does not have any better common ancestor is a best common
21       ancestor, i.e. a merge base. Note that there can be more than one merge
22       base for a pair of commits.
23

OPERATION MODES

25       As the most common special case, specifying only two commits on the
26       command line means computing the merge base between the given two
27       commits.
28
29       More generally, among the two commits to compute the merge base from,
30       one is specified by the first commit argument on the command line; the
31       other commit is a (possibly hypothetical) commit that is a merge across
32       all the remaining commits on the command line.
33
34       As a consequence, the merge base is not necessarily contained in each
35       of the commit arguments if more than two commits are specified. This is
36       different from git-show-branch(1) when used with the --merge-base
37       option.
38
39       --octopus
40           Compute the best common ancestors of all supplied commits, in
41           preparation for an n-way merge. This mimics the behavior of git
42           show-branch --merge-base.
43
44       --independent
45           Instead of printing merge bases, print a minimal subset of the
46           supplied commits with the same ancestors. In other words, among the
47           commits given, list those which cannot be reached from any other.
48           This mimics the behavior of git show-branch --independent.
49
50       --is-ancestor
51           Check if the first <commit> is an ancestor of the second <commit>,
52           and exit with status 0 if true, or with status 1 if not. Errors are
53           signaled by a non-zero status that is not 1.
54
55       --fork-point
56           Find the point at which a branch (or any history that leads to
57           <commit>) forked from another branch (or any reference) <ref>. This
58           does not just look for the common ancestor of the two commits, but
59           also takes into account the reflog of <ref> to see if the history
60           leading to <commit> forked from an earlier incarnation of the
61           branch <ref> (see discussion on this mode below).
62

OPTIONS

64       -a, --all
65           Output all merge bases for the commits, instead of just one.
66

DISCUSSION

68       Given two commits A and B, git merge-base A B will output a commit
69       which is reachable from both A and B through the parent relationship.
70
71       For example, with this topology:
72
73                    o---o---o---B
74                   /
75           ---o---1---o---o---o---A
76
77       the merge base between A and B is 1.
78
79       Given three commits A, B and C, git merge-base A B C will compute the
80       merge base between A and a hypothetical commit M, which is a merge
81       between B and C. For example, with this topology:
82
83                  o---o---o---o---C
84                 /
85                /   o---o---o---B
86               /   /
87           ---2---1---o---o---o---A
88
89       the result of git merge-base A B C is 1. This is because the equivalent
90       topology with a merge commit M between B and C is:
91
92                  o---o---o---o---o
93                 /                 \
94                /   o---o---o---o---M
95               /   /
96           ---2---1---o---o---o---A
97
98       and the result of git merge-base A M is 1. Commit 2 is also a common
99       ancestor between A and M, but 1 is a better common ancestor, because 2
100       is an ancestor of 1. Hence, 2 is not a merge base.
101
102       The result of git merge-base --octopus A B C is 2, because 2 is the
103       best common ancestor of all commits.
104
105       When the history involves criss-cross merges, there can be more than
106       one best common ancestor for two commits. For example, with this
107       topology:
108
109           ---1---o---A
110               \ /
111                X
112               / \
113           ---2---o---o---B
114
115       both 1 and 2 are merge-bases of A and B. Neither one is better than the
116       other (both are best merge bases). When the --all option is not given,
117       it is unspecified which best one is output.
118
119       A common idiom to check "fast-forward-ness" between two commits A and B
120       is (or at least used to be) to compute the merge base between A and B,
121       and check if it is the same as A, in which case, A is an ancestor of B.
122       You will see this idiom used often in older scripts.
123
124           A=$(git rev-parse --verify A)
125           if test "$A" = "$(git merge-base A B)"
126           then
127                   ... A is an ancestor of B ...
128           fi
129
130       In modern git, you can say this in a more direct way:
131
132           if git merge-base --is-ancestor A B
133           then
134                   ... A is an ancestor of B ...
135           fi
136
137       instead.
138

DISCUSSION ON FORK-POINT MODE

140       After working on the topic branch created with git switch -c topic
141       origin/master, the history of remote-tracking branch origin/master may
142       have been rewound and rebuilt, leading to a history of this shape:
143
144                            o---B2
145                           /
146           ---o---o---B1--o---o---o---B (origin/master)
147                   \
148                    B0
149                     \
150                      D0---D1---D (topic)
151
152       where origin/master used to point at commits B0, B1, B2 and now it
153       points at B, and your topic branch was started on top of it back when
154       origin/master was at B0, and you built three commits, D0, D1, and D, on
155       top of it. Imagine that you now want to rebase the work you did on the
156       topic on top of the updated origin/master.
157
158       In such a case, git merge-base origin/master topic would return the
159       parent of B0 in the above picture, but B0^..D is not the range of
160       commits you would want to replay on top of B (it includes B0, which is
161       not what you wrote; it is a commit the other side discarded when it
162       moved its tip from B0 to B1).
163
164       git merge-base --fork-point origin/master topic is designed to help in
165       such a case. It takes not only B but also B0, B1, and B2 (i.e. old tips
166       of the remote-tracking branches your repository’s reflog knows about)
167       into account to see on which commit your topic branch was built and
168       finds B0, allowing you to replay only the commits on your topic,
169       excluding the commits the other side later discarded.
170
171       Hence
172
173           $ fork_point=$(git merge-base --fork-point origin/master topic)
174
175       will find B0, and
176
177           $ git rebase --onto origin/master $fork_point topic
178
179       will replay D0, D1 and D on top of B to create a new history of this
180       shape:
181
182                            o---B2
183                           /
184           ---o---o---B1--o---o---o---B (origin/master)
185                   \                   \
186                    B0                  D0'--D1'--D' (topic - updated)
187                     \
188                      D0---D1---D (topic - old)
189
190       A caveat is that older reflog entries in your repository may be expired
191       by git gc. If B0 no longer appears in the reflog of the remote-tracking
192       branch origin/master, the --fork-point mode obviously cannot find it
193       and fails, avoiding to give a random and useless result (such as the
194       parent of B0, like the same command without the --fork-point option
195       gives).
196
197       Also, the remote-tracking branch you use the --fork-point mode with
198       must be the one your topic forked from its tip. If you forked from an
199       older commit than the tip, this mode would not find the fork point
200       (imagine in the above sample history B0 did not exist, origin/master
201       started at B1, moved to B2 and then B, and you forked your topic at
202       origin/master^ when origin/master was B1; the shape of the history
203       would be the same as above, without B0, and the parent of B1 is what
204       git merge-base origin/master topic correctly finds, but the
205       --fork-point mode will not, because it is not one of the commits that
206       used to be at the tip of origin/master).
207

SEE ALSO

209       git-rev-list(1), git-show-branch(1), git-merge(1)
210

GIT

212       Part of the git(1) suite
213
214
215
216Git 2.24.1                        12/10/2019                 GIT-MERGE-BASE(1)
Impressum