blob: 5ab957cfbc1420d5dca07a21dbf6708777ee754e [file] [log] [blame]
David Greaves2cf565c2005-05-10 22:32:30 +01001git-merge-base(1)
2=================
David Greaves2cf565c2005-05-10 22:32:30 +01003
4NAME
5----
Junio C Hamanoc3f0baa2007-01-18 15:53:37 -08006git-merge-base - Find as good common ancestors as possible for a merge
David Greaves2cf565c2005-05-10 22:32:30 +01007
8
9SYNOPSIS
10--------
Jonathan Niedera1e0ad72010-08-17 02:01:54 -050011[verse]
Ævar Arnfjörð Bjarmasone2f4e7e2022-10-13 17:39:06 +020012'git merge-base' [-a | --all] <commit> <commit>...
13'git merge-base' [-a | --all] --octopus <commit>...
Junio C Hamano5907cda2012-08-30 14:52:20 -070014'git merge-base' --is-ancestor <commit> <commit>
Jonathan Niedera1e0ad72010-08-17 02:01:54 -050015'git merge-base' --independent <commit>...
Junio C Hamanod96855f2013-10-23 16:47:32 -070016'git merge-base' --fork-point <ref> [<commit>]
David Greaves2cf565c2005-05-10 22:32:30 +010017
18DESCRIPTION
19-----------
Fredrik Kuivinen2aa83962006-05-16 07:58:15 +020020
Elijah Newren0a4f0512023-10-08 06:45:17 +000021'git merge-base' finds the best common ancestor(s) between two commits to use
Junio C Hamano99f1c042008-07-30 07:04:43 +020022in a three-way merge. One common ancestor is 'better' than another common
23ancestor if the latter is an ancestor of the former. A common ancestor
Ralf Wildenhues29b802a2008-12-09 07:23:51 +010024that does not have any better common ancestor is a 'best common
Junio C Hamano99f1c042008-07-30 07:04:43 +020025ancestor', i.e. a 'merge base'. Note that there can be more than one
Ralf Wildenhues29b802a2008-12-09 07:23:51 +010026merge base for a pair of commits.
Fredrik Kuivinen2aa83962006-05-16 07:58:15 +020027
Junio C Hamanod96855f2013-10-23 16:47:32 -070028OPERATION MODES
29---------------
Jonathan Niederded7e042011-04-15 10:38:55 +020030
Elijah Newren9a9fd282023-10-08 06:45:20 +000031In the most common special case, specifying only two commits on the
Jonathan Niederded7e042011-04-15 10:38:55 +020032command line means computing the merge base between the given two commits.
33
34More generally, among the two commits to compute the merge base from,
35one is specified by the first commit argument on the command line;
36the other commit is a (possibly hypothetical) commit that is a merge
37across all the remaining commits on the command line.
David Greaves2cf565c2005-05-10 22:32:30 +010038
Michael J Gruberf621a842009-08-05 09:59:20 +020039As a consequence, the 'merge base' is not necessarily contained in each of the
40commit arguments if more than two commits are specified. This is different
41from linkgit:git-show-branch[1] when used with the `--merge-base` option.
42
Jonathan Niederaa8f98c2010-08-17 02:01:15 -050043--octopus::
44 Compute the best common ancestors of all supplied commits,
45 in preparation for an n-way merge. This mimics the behavior
46 of 'git show-branch --merge-base'.
47
Jonathan Niedera1e0ad72010-08-17 02:01:54 -050048--independent::
49 Instead of printing merge bases, print a minimal subset of
50 the supplied commits with the same ancestors. In other words,
51 among the commits given, list those which cannot be reached
52 from any other. This mimics the behavior of 'git show-branch
53 --independent'.
54
Junio C Hamano5907cda2012-08-30 14:52:20 -070055--is-ancestor::
56 Check if the first <commit> is an ancestor of the second <commit>,
57 and exit with status 0 if true, or with status 1 if not.
58 Errors are signaled by a non-zero status that is not 1.
59
Junio C Hamanod96855f2013-10-23 16:47:32 -070060--fork-point::
61 Find the point at which a branch (or any history that leads
62 to <commit>) forked from another branch (or any reference)
63 <ref>. This does not just look for the common ancestor of
64 the two commits, but also takes into account the reflog of
65 <ref> to see if the history leading to <commit> forked from
66 an earlier incarnation of the branch <ref> (see discussion
Elijah Newren9a9fd282023-10-08 06:45:20 +000067 of this mode below).
Junio C Hamano5907cda2012-08-30 14:52:20 -070068
Jonathan Niederded7e042011-04-15 10:38:55 +020069OPTIONS
70-------
71-a::
72--all::
73 Output all merge bases for the commits, instead of just one.
74
Junio C Hamano99f1c042008-07-30 07:04:43 +020075DISCUSSION
76----------
77
78Given two commits 'A' and 'B', `git merge-base A B` will output a commit
79which is reachable from both 'A' and 'B' through the parent relationship.
80
81For example, with this topology:
82
Martin Ågren922a2c92019-09-07 16:12:48 +020083....
84 o---o---o---B
85 /
86---o---1---o---o---o---A
87....
Junio C Hamano99f1c042008-07-30 07:04:43 +020088
89the merge base between 'A' and 'B' is '1'.
90
Elijah Newren4d542682023-10-08 06:45:24 +000091Given three commits 'A', 'B', and 'C', `git merge-base A B C` will compute the
Ralf Wildenhues29b802a2008-12-09 07:23:51 +010092merge base between 'A' and a hypothetical commit 'M', which is a merge
Junio C Hamano99f1c042008-07-30 07:04:43 +020093between 'B' and 'C'. For example, with this topology:
94
Martin Ågren922a2c92019-09-07 16:12:48 +020095....
96 o---o---o---o---C
97 /
98 / o---o---o---B
99 / /
100---2---1---o---o---o---A
101....
Junio C Hamano99f1c042008-07-30 07:04:43 +0200102
103the result of `git merge-base A B C` is '1'. This is because the
104equivalent topology with a merge commit 'M' between 'B' and 'C' is:
105
106
Martin Ågren922a2c92019-09-07 16:12:48 +0200107....
108 o---o---o---o---o
109 / \
110 / o---o---o---o---M
111 / /
112---2---1---o---o---o---A
113....
Junio C Hamano99f1c042008-07-30 07:04:43 +0200114
115and the result of `git merge-base A M` is '1'. Commit '2' is also a
116common ancestor between 'A' and 'M', but '1' is a better common ancestor,
117because '2' is an ancestor of '1'. Hence, '2' is not a merge base.
118
Vincent van Ravesteijn57294822011-04-15 10:34:03 +0200119The result of `git merge-base --octopus A B C` is '2', because '2' is
120the best common ancestor of all commits.
121
Junio C Hamano99f1c042008-07-30 07:04:43 +0200122When the history involves criss-cross merges, there can be more than one
Ralf Wildenhues29b802a2008-12-09 07:23:51 +0100123'best' common ancestor for two commits. For example, with this topology:
Junio C Hamano99f1c042008-07-30 07:04:43 +0200124
Martin Ågren922a2c92019-09-07 16:12:48 +0200125....
126---1---o---A
127 \ /
128 X
129 / \
130---2---o---o---B
131....
Junio C Hamano99f1c042008-07-30 07:04:43 +0200132
Elijah Newrenf22fdf32023-10-08 06:45:18 +0000133both '1' and '2' are merge bases of A and B. Neither one is better than
Ralf Wildenhues29b802a2008-12-09 07:23:51 +0100134the other (both are 'best' merge bases). When the `--all` option is not given,
Junio C Hamano99f1c042008-07-30 07:04:43 +0200135it is unspecified which best one is output.
David Greaves2cf565c2005-05-10 22:32:30 +0100136
Junio C Hamano5907cda2012-08-30 14:52:20 -0700137A common idiom to check "fast-forward-ness" between two commits A
138and B is (or at least used to be) to compute the merge base between
139A and B, and check if it is the same as A, in which case, A is an
140ancestor of B. You will see this idiom used often in older scripts.
141
Martin Ågren922a2c92019-09-07 16:12:48 +0200142....
143A=$(git rev-parse --verify A)
144if test "$A" = "$(git merge-base A B)"
145then
146 ... A is an ancestor of B ...
147fi
148....
Junio C Hamano5907cda2012-08-30 14:52:20 -0700149
150In modern git, you can say this in a more direct way:
151
Martin Ågren922a2c92019-09-07 16:12:48 +0200152....
153if git merge-base --is-ancestor A B
154then
155 ... A is an ancestor of B ...
156fi
157....
Junio C Hamano5907cda2012-08-30 14:52:20 -0700158
159instead.
160
Junio C Hamanod96855f2013-10-23 16:47:32 -0700161Discussion on fork-point mode
162-----------------------------
163
Nguyễn Thái Ngọc Duy328c6cb2019-03-29 17:39:19 +0700164After working on the `topic` branch created with `git switch -c
Junio C Hamanod96855f2013-10-23 16:47:32 -0700165topic origin/master`, the history of remote-tracking branch
166`origin/master` may have been rewound and rebuilt, leading to a
167history of this shape:
168
Martin Ågren922a2c92019-09-07 16:12:48 +0200169....
170 o---B2
171 /
172---o---o---B1--o---o---o---B (origin/master)
173 \
174 B0
175 \
176 D0---D1---D (topic)
177....
Junio C Hamanod96855f2013-10-23 16:47:32 -0700178
Junio C Hamano6d1700b2017-11-09 11:49:45 +0900179where `origin/master` used to point at commits B0, B1, B2 and now it
Junio C Hamanod96855f2013-10-23 16:47:32 -0700180points at B, and your `topic` branch was started on top of it back
Junio C Hamano6d1700b2017-11-09 11:49:45 +0900181when `origin/master` was at B0, and you built three commits, D0, D1,
182and D, on top of it. Imagine that you now want to rebase the work
183you did on the topic on top of the updated origin/master.
184
185In such a case, `git merge-base origin/master topic` would return the
186parent of B0 in the above picture, but B0^..D is *not* the range of
187commits you would want to replay on top of B (it includes B0, which
188is not what you wrote; it is a commit the other side discarded when
189it moved its tip from B0 to B1).
190
191`git merge-base --fork-point origin/master topic` is designed to
192help in such a case. It takes not only B but also B0, B1, and B2
193(i.e. old tips of the remote-tracking branches your repository's
194reflog knows about) into account to see on which commit your topic
195branch was built and finds B0, allowing you to replay only the
196commits on your topic, excluding the commits the other side later
197discarded.
198
199Hence
Junio C Hamanod96855f2013-10-23 16:47:32 -0700200
201 $ fork_point=$(git merge-base --fork-point origin/master topic)
Junio C Hamano6d1700b2017-11-09 11:49:45 +0900202
203will find B0, and
204
Junio C Hamanod96855f2013-10-23 16:47:32 -0700205 $ git rebase --onto origin/master $fork_point topic
206
Elijah Newren4d542682023-10-08 06:45:24 +0000207will replay D0, D1, and D on top of B to create a new history of this
Junio C Hamano6d1700b2017-11-09 11:49:45 +0900208shape:
209
Martin Ågren922a2c92019-09-07 16:12:48 +0200210....
211 o---B2
212 /
213---o---o---B1--o---o---o---B (origin/master)
214 \ \
215 B0 D0'--D1'--D' (topic - updated)
216 \
217 D0---D1---D (topic - old)
218....
Junio C Hamano6d1700b2017-11-09 11:49:45 +0900219
220A caveat is that older reflog entries in your repository may be
221expired by `git gc`. If B0 no longer appears in the reflog of the
222remote-tracking branch `origin/master`, the `--fork-point` mode
223obviously cannot find it and fails, avoiding to give a random and
224useless result (such as the parent of B0, like the same command
225without the `--fork-point` option gives).
226
227Also, the remote-tracking branch you use the `--fork-point` mode
228with must be the one your topic forked from its tip. If you forked
229from an older commit than the tip, this mode would not find the fork
230point (imagine in the above sample history B0 did not exist,
231origin/master started at B1, moved to B2 and then B, and you forked
232your topic at origin/master^ when origin/master was B1; the shape of
233the history would be the same as above, without B0, and the parent
234of B1 is what `git merge-base origin/master topic` correctly finds,
235but the `--fork-point` mode will not, because it is not one of the
236commits that used to be at the tip of origin/master).
237
Junio C Hamano5907cda2012-08-30 14:52:20 -0700238
Jonathan Nieder1846e9e2010-08-17 01:59:55 -0500239See also
240--------
241linkgit:git-rev-list[1],
242linkgit:git-show-branch[1],
243linkgit:git-merge[1]
244
David Greaves2cf565c2005-05-10 22:32:30 +0100245GIT
246---
Christian Couder9e1f0a82008-06-06 09:07:32 +0200247Part of the linkgit:git[1] suite