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