2 .\" Title: git-merge-base
3 .\" Author: [FIXME: author] [see http://www.docbook.org/tdg5/en/html/author]
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
7 .\" Source: Git 2.45.0.rc0.3.g00e10ef10e
10 .TH "GIT\-MERGE\-BASE" "1" "2024\-04\-22" "Git 2\&.45\&.0\&.rc0\&.3\&.g00" "Git Manual"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
25 .\" disable justification (adjust text to left margin only)
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
31 git-merge-base \- Find as good common ancestors as possible for a merge
35 \fIgit merge\-base\fR [\-a | \-\-all] <commit> <commit>\&...
36 \fIgit merge\-base\fR [\-a | \-\-all] \-\-octopus <commit>\&...
37 \fIgit merge\-base\fR \-\-is\-ancestor <commit> <commit>
38 \fIgit merge\-base\fR \-\-independent <commit>\&...
39 \fIgit merge\-base\fR \-\-fork\-point <ref> [<commit>]
44 \fIgit merge\-base\fR finds the best common ancestor(s) between two commits to use in a three\-way merge\&. One common ancestor is \fIbetter\fR than another common ancestor if the latter is an ancestor of the former\&. A common ancestor that does not have any better common ancestor is a \fIbest common ancestor\fR, i\&.e\&. a \fImerge base\fR\&. Note that there can be more than one merge base for a pair of commits\&.
47 In the most common special case, specifying only two commits on the command line means computing the merge base between the given two commits\&.
49 More generally, among the two commits to compute the merge base from, one is specified by the first commit argument on the command line; the other commit is a (possibly hypothetical) commit that is a merge across all the remaining commits on the command line\&.
51 As a consequence, the \fImerge base\fR is not necessarily contained in each of the commit arguments if more than two commits are specified\&. This is different from \fBgit-show-branch\fR(1) when used with the \fB\-\-merge\-base\fR option\&.
55 Compute the best common ancestors of all supplied commits, in preparation for an n\-way merge\&. This mimics the behavior of
56 \fIgit show\-branch \-\-merge\-base\fR\&.
61 Instead of printing merge bases, print a minimal subset of the supplied commits with the same ancestors\&. In other words, among the commits given, list those which cannot be reached from any other\&. This mimics the behavior of
62 \fIgit show\-branch \-\-independent\fR\&.
67 Check if the first <commit> is an ancestor of the second <commit>, 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\&.
72 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 of this mode below)\&.
78 Output all merge bases for the commits, instead of just one\&.
82 Given two commits \fIA\fR and \fIB\fR, \fBgit merge\-base A B\fR will output a commit which is reachable from both \fIA\fR and \fIB\fR through the parent relationship\&.
84 For example, with this topology:
90 o\-\-\-o\-\-\-o\-\-\-B
92 \-\-\-o\-\-\-1\-\-\-o\-\-\-o\-\-\-o\-\-\-A
98 the merge base between \fIA\fR and \fIB\fR is \fI1\fR\&.
100 Given three commits \fIA\fR, \fIB\fR, and \fIC\fR, \fBgit merge\-base A B C\fR will compute the merge base between \fIA\fR and a hypothetical commit \fIM\fR, which is a merge between \fIB\fR and \fIC\fR\&. For example, with this topology:
106 o\-\-\-o\-\-\-o\-\-\-o\-\-\-C
108 / o\-\-\-o\-\-\-o\-\-\-B
110 \-\-\-2\-\-\-1\-\-\-o\-\-\-o\-\-\-o\-\-\-A
116 the result of \fBgit merge\-base A B C\fR is \fI1\fR\&. This is because the equivalent topology with a merge commit \fIM\fR between \fIB\fR and \fIC\fR is:
122 o\-\-\-o\-\-\-o\-\-\-o\-\-\-o
124 / o\-\-\-o\-\-\-o\-\-\-o\-\-\-M
126 \-\-\-2\-\-\-1\-\-\-o\-\-\-o\-\-\-o\-\-\-A
132 and the result of \fBgit merge\-base A M\fR is \fI1\fR\&. Commit \fI2\fR is also a common ancestor between \fIA\fR and \fIM\fR, but \fI1\fR is a better common ancestor, because \fI2\fR is an ancestor of \fI1\fR\&. Hence, \fI2\fR is not a merge base\&.
134 The result of \fBgit merge\-base \-\-octopus A B C\fR is \fI2\fR, because \fI2\fR is the best common ancestor of all commits\&.
136 When the history involves criss\-cross merges, there can be more than one \fIbest\fR common ancestor for two commits\&. For example, with this topology:
142 \-\-\-1\-\-\-o\-\-\-A
146 \-\-\-2\-\-\-o\-\-\-o\-\-\-B
152 both \fI1\fR and \fI2\fR are merge bases of A and B\&. Neither one is better than the other (both are \fIbest\fR merge bases)\&. When the \fB\-\-all\fR option is not given, it is unspecified which best one is output\&.
154 A common idiom to check "fast\-forward\-ness" between two commits A and B is (or at least used to be) to compute the merge base between A and B, and check if it is the same as A, in which case, A is an ancestor of B\&. You will see this idiom used often in older scripts\&.
160 A=$(git rev\-parse \-\-verify A)
161 if test "$A" = "$(git merge\-base A B)"
163 \&.\&.\&. A is an ancestor of B \&.\&.\&.
170 In modern git, you can say this in a more direct way:
176 if git merge\-base \-\-is\-ancestor A B
178 \&.\&.\&. A is an ancestor of B \&.\&.\&.
186 .SH "DISCUSSION ON FORK\-POINT MODE"
188 After working on the \fBtopic\fR branch created with \fBgit switch \-c topic origin/master\fR, the history of remote\-tracking branch \fBorigin/master\fR may have been rewound and rebuilt, leading to a history of this shape:
196 \-\-\-o\-\-\-o\-\-\-B1\-\-o\-\-\-o\-\-\-o\-\-\-B (origin/master)
200 D0\-\-\-D1\-\-\-D (topic)
206 where \fBorigin/master\fR used to point at commits B0, B1, B2 and now it points at B, and your \fBtopic\fR branch was started on top of it back when \fBorigin/master\fR was 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 the topic on top of the updated origin/master\&.
208 In such a case, \fBgit merge\-base origin/master topic\fR would return the parent of B0 in the above picture, but B0^\&.\&.D is \fBnot\fR 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)\&.
210 \fBgit merge\-base \-\-fork\-point origin/master topic\fR is 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\(cqs 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\&.
218 $ fork_point=$(git merge\-base \-\-fork\-point origin/master topic)
230 $ git rebase \-\-onto origin/master $fork_point topic
236 will replay D0, D1, and D on top of B to create a new history of this shape:
244 \-\-\-o\-\-\-o\-\-\-B1\-\-o\-\-\-o\-\-\-o\-\-\-B (origin/master)
246 B0 D0\*(Aq\-\-D1\*(Aq\-\-D\*(Aq (topic \- updated)
248 D0\-\-\-D1\-\-\-D (topic \- old)
254 A caveat is that older reflog entries in your repository may be expired by \fBgit gc\fR\&. If B0 no longer appears in the reflog of the remote\-tracking branch \fBorigin/master\fR, the \fB\-\-fork\-point\fR mode 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 \fB\-\-fork\-point\fR option gives)\&.
256 Also, the remote\-tracking branch you use the \fB\-\-fork\-point\fR mode 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/master started at B1, moved to B2 and then B, and you forked your topic at origin/master^ when origin/master was B1; the shape of the history would be the same as above, without B0, and the parent of B1 is what \fBgit merge\-base origin/master topic\fR correctly finds, but the \fB\-\-fork\-point\fR mode will not, because it is not one of the commits that used to be at the tip of origin/master)\&.
259 \fBgit-rev-list\fR(1), \fBgit-show-branch\fR(1), \fBgit-merge\fR(1)
262 Part of the \fBgit\fR(1) suite