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

DESCRIPTION

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

OPERATION MODES

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

OPTIONS

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

DISCUSSION

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

DISCUSSION ON FORK-POINT MODE

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

SEE ALSO

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

GIT

211       Part of the git(1) suite
212
213
214
215Git 2.26.2                        2020-04-20                 GIT-MERGE-BASE(1)
Impressum