]> git.ipfire.org Git - thirdparty/git.git/blame - Documentation/git-merge-base.txt
merge-base: use OPT_CMDMODE and clarify the command line parsing
[thirdparty/git.git] / Documentation / git-merge-base.txt
CommitLineData
2cf565c5
DG
1git-merge-base(1)
2=================
2cf565c5
DG
3
4NAME
5----
c3f0baac 6git-merge-base - Find as good common ancestors as possible for a merge
2cf565c5
DG
7
8
9SYNOPSIS
10--------
a1e0ad78 11[verse]
57294824
VR
12'git merge-base' [-a|--all] <commit> <commit>...
13'git merge-base' [-a|--all] --octopus <commit>...
5907cda1 14'git merge-base' --is-ancestor <commit> <commit>
a1e0ad78 15'git merge-base' --independent <commit>...
2cf565c5
DG
16
17DESCRIPTION
18-----------
2aa83961 19
995bdc73 20'git merge-base' finds best common ancestor(s) between two commits to use
99f1c04b
JH
21in a three-way merge. One common ancestor is 'better' than another common
22ancestor if the latter is an ancestor of the former. A common ancestor
29b802aa 23that does not have any better common ancestor is a 'best common
99f1c04b 24ancestor', i.e. a 'merge base'. Note that there can be more than one
29b802aa 25merge base for a pair of commits.
2aa83961 26
ded7e049
JN
27OPERATION MODE
28--------------
29
30As the most common special case, specifying only two commits on the
31command line means computing the merge base between the given two commits.
32
33More generally, among the two commits to compute the merge base from,
34one is specified by the first commit argument on the command line;
35the other commit is a (possibly hypothetical) commit that is a merge
36across all the remaining commits on the command line.
2cf565c5 37
f621a845
MG
38As a consequence, the 'merge base' is not necessarily contained in each of the
39commit arguments if more than two commits are specified. This is different
40from linkgit:git-show-branch[1] when used with the `--merge-base` option.
41
aa8f98c1
JN
42--octopus::
43 Compute the best common ancestors of all supplied commits,
44 in preparation for an n-way merge. This mimics the behavior
45 of 'git show-branch --merge-base'.
46
a1e0ad78
JN
47--independent::
48 Instead of printing merge bases, print a minimal subset of
49 the supplied commits with the same ancestors. In other words,
50 among the commits given, list those which cannot be reached
51 from any other. This mimics the behavior of 'git show-branch
52 --independent'.
53
5907cda1
JH
54--is-ancestor::
55 Check if the first <commit> is an ancestor of the second <commit>,
56 and exit with status 0 if true, or with status 1 if not.
57 Errors are signaled by a non-zero status that is not 1.
58
59
ded7e049
JN
60OPTIONS
61-------
62-a::
63--all::
64 Output all merge bases for the commits, instead of just one.
65
99f1c04b
JH
66DISCUSSION
67----------
68
69Given two commits 'A' and 'B', `git merge-base A B` will output a commit
70which is reachable from both 'A' and 'B' through the parent relationship.
71
72For example, with this topology:
73
74 o---o---o---B
75 /
76 ---o---1---o---o---o---A
77
78the merge base between 'A' and 'B' is '1'.
79
80Given three commits 'A', 'B' and 'C', `git merge-base A B C` will compute the
29b802aa 81merge base between 'A' and a hypothetical commit 'M', which is a merge
99f1c04b
JH
82between 'B' and 'C'. For example, with this topology:
83
84 o---o---o---o---C
85 /
86 / o---o---o---B
87 / /
88 ---2---1---o---o---o---A
89
90the result of `git merge-base A B C` is '1'. This is because the
91equivalent topology with a merge commit 'M' between 'B' and 'C' is:
92
93
94 o---o---o---o---o
95 / \
96 / o---o---o---o---M
97 / /
98 ---2---1---o---o---o---A
99
100and the result of `git merge-base A M` is '1'. Commit '2' is also a
101common ancestor between 'A' and 'M', but '1' is a better common ancestor,
102because '2' is an ancestor of '1'. Hence, '2' is not a merge base.
103
57294824
VR
104The result of `git merge-base --octopus A B C` is '2', because '2' is
105the best common ancestor of all commits.
106
99f1c04b 107When the history involves criss-cross merges, there can be more than one
29b802aa 108'best' common ancestor for two commits. For example, with this topology:
99f1c04b
JH
109
110 ---1---o---A
111 \ /
112 X
113 / \
114 ---2---o---o---B
115
29b802aa
RW
116both '1' and '2' are merge-bases of A and B. Neither one is better than
117the other (both are 'best' merge bases). When the `--all` option is not given,
99f1c04b 118it is unspecified which best one is output.
2cf565c5 119
5907cda1
JH
120A common idiom to check "fast-forward-ness" between two commits A
121and B is (or at least used to be) to compute the merge base between
122A and B, and check if it is the same as A, in which case, A is an
123ancestor of B. You will see this idiom used often in older scripts.
124
125 A=$(git rev-parse --verify A)
126 if test "$A" = "$(git merge-base A B)"
127 then
128 ... A is an ancestor of B ...
129 fi
130
131In modern git, you can say this in a more direct way:
132
133 if git merge-base --is-ancestor A B
134 then
135 ... A is an ancestor of B ...
136 fi
137
138instead.
139
140
1846e9ed
JN
141See also
142--------
143linkgit:git-rev-list[1],
144linkgit:git-show-branch[1],
145linkgit:git-merge[1]
146
2cf565c5
DG
147GIT
148---
9e1f0a85 149Part of the linkgit:git[1] suite