Autogenerated manpages for v2.45.0-rc0-48-g10f128
[git-manpages.git] / man1 / git-merge-base.1
blob5d2c2fb20a613fa89fb08fb7cf936e7512ba43fb
1 '\" t
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/>
5 .\"      Date: 2024-04-23
6 .\"    Manual: Git Manual
7 .\"    Source: Git 2.45.0.rc0.48.g10f1281498
8 .\"  Language: English
9 .\"
10 .TH "GIT\-MERGE\-BASE" "1" "2024\-04\-23" "Git 2\&.45\&.0\&.rc0\&.48\&.g1" "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 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 git-merge-base \- Find as good common ancestors as possible for a merge
32 .SH "SYNOPSIS"
33 .sp
34 .nf
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>]
40 .fi
41 .sp
42 .SH "DESCRIPTION"
43 .sp
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\&.
45 .SH "OPERATION MODES"
46 .sp
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\&.
48 .sp
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\&.
50 .sp
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\&.
52 .PP
53 \-\-octopus
54 .RS 4
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\&.
57 .RE
58 .PP
59 \-\-independent
60 .RS 4
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\&.
63 .RE
64 .PP
65 \-\-is\-ancestor
66 .RS 4
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\&.
68 .RE
69 .PP
70 \-\-fork\-point
71 .RS 4
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)\&.
73 .RE
74 .SH "OPTIONS"
75 .PP
76 \-a, \-\-all
77 .RS 4
78 Output all merge bases for the commits, instead of just one\&.
79 .RE
80 .SH "DISCUSSION"
81 .sp
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\&.
83 .sp
84 For example, with this topology:
85 .sp
86 .if n \{\
87 .RS 4
88 .\}
89 .nf
90          o\-\-\-o\-\-\-o\-\-\-B
91         /
92 \-\-\-o\-\-\-1\-\-\-o\-\-\-o\-\-\-o\-\-\-A
93 .fi
94 .if n \{\
95 .RE
96 .\}
97 .sp
98 the merge base between \fIA\fR and \fIB\fR is \fI1\fR\&.
99 .sp
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:
102 .if n \{\
103 .RS 4
106        o\-\-\-o\-\-\-o\-\-\-o\-\-\-C
107       /
108      /   o\-\-\-o\-\-\-o\-\-\-B
109     /   /
110 \-\-\-2\-\-\-1\-\-\-o\-\-\-o\-\-\-o\-\-\-A
112 .if n \{\
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:
118 .if n \{\
119 .RS 4
122        o\-\-\-o\-\-\-o\-\-\-o\-\-\-o
123       /                 \e
124      /   o\-\-\-o\-\-\-o\-\-\-o\-\-\-M
125     /   /
126 \-\-\-2\-\-\-1\-\-\-o\-\-\-o\-\-\-o\-\-\-A
128 .if n \{\
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:
138 .if n \{\
139 .RS 4
142 \-\-\-1\-\-\-o\-\-\-A
143     \e /
144      X
145     / \e
146 \-\-\-2\-\-\-o\-\-\-o\-\-\-B
148 .if n \{\
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\&.
156 .if n \{\
157 .RS 4
160 A=$(git rev\-parse \-\-verify A)
161 if test "$A" = "$(git merge\-base A B)"
162 then
163         \&.\&.\&. A is an ancestor of B \&.\&.\&.
166 .if n \{\
170 In modern git, you can say this in a more direct way:
172 .if n \{\
173 .RS 4
176 if git merge\-base \-\-is\-ancestor A B
177 then
178         \&.\&.\&. A is an ancestor of B \&.\&.\&.
181 .if n \{\
185 instead\&.
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:
190 .if n \{\
191 .RS 4
194                  o\-\-\-B2
195                 /
196 \-\-\-o\-\-\-o\-\-\-B1\-\-o\-\-\-o\-\-\-o\-\-\-B (origin/master)
197         \e
198          B0
199           \e
200            D0\-\-\-D1\-\-\-D (topic)
202 .if n \{\
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\&.
212 Hence
214 .if n \{\
215 .RS 4
218 $ fork_point=$(git merge\-base \-\-fork\-point origin/master topic)
220 .if n \{\
224 will find B0, and
226 .if n \{\
227 .RS 4
230 $ git rebase \-\-onto origin/master $fork_point topic
232 .if n \{\
236 will replay D0, D1, and D on top of B to create a new history of this shape:
238 .if n \{\
239 .RS 4
242                  o\-\-\-B2
243                 /
244 \-\-\-o\-\-\-o\-\-\-B1\-\-o\-\-\-o\-\-\-o\-\-\-B (origin/master)
245         \e                   \e
246          B0                  D0\*(Aq\-\-D1\*(Aq\-\-D\*(Aq (topic \- updated)
247           \e
248            D0\-\-\-D1\-\-\-D (topic \- old)
250 .if n \{\
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)\&.
257 .SH "SEE ALSO"
259 \fBgit-rev-list\fR(1), \fBgit-show-branch\fR(1), \fBgit-merge\fR(1)
260 .SH "GIT"
262 Part of the \fBgit\fR(1) suite