]>
Commit | Line | Data |
---|---|---|
2cf565c5 DG |
1 | git-merge-base(1) |
2 | ================= | |
2cf565c5 DG |
3 | |
4 | NAME | |
5 | ---- | |
c3f0baac | 6 | git-merge-base - Find as good common ancestors as possible for a merge |
2cf565c5 DG |
7 | |
8 | ||
9 | SYNOPSIS | |
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 | |
17 | DESCRIPTION | |
18 | ----------- | |
2aa83961 | 19 | |
995bdc73 | 20 | 'git merge-base' finds best common ancestor(s) between two commits to use |
99f1c04b JH |
21 | in a three-way merge. One common ancestor is 'better' than another common |
22 | ancestor if the latter is an ancestor of the former. A common ancestor | |
29b802aa | 23 | that does not have any better common ancestor is a 'best common |
99f1c04b | 24 | ancestor', i.e. a 'merge base'. Note that there can be more than one |
29b802aa | 25 | merge base for a pair of commits. |
2aa83961 | 26 | |
ded7e049 JN |
27 | OPERATION MODE |
28 | -------------- | |
29 | ||
30 | As the most common special case, specifying only two commits on the | |
31 | command line means computing the merge base between the given two commits. | |
32 | ||
33 | More generally, among the two commits to compute the merge base from, | |
34 | one is specified by the first commit argument on the command line; | |
35 | the other commit is a (possibly hypothetical) commit that is a merge | |
36 | across all the remaining commits on the command line. | |
2cf565c5 | 37 | |
f621a845 MG |
38 | As a consequence, the 'merge base' is not necessarily contained in each of the |
39 | commit arguments if more than two commits are specified. This is different | |
40 | from 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 |
60 | OPTIONS |
61 | ------- | |
62 | -a:: | |
63 | --all:: | |
64 | Output all merge bases for the commits, instead of just one. | |
65 | ||
99f1c04b JH |
66 | DISCUSSION |
67 | ---------- | |
68 | ||
69 | Given two commits 'A' and 'B', `git merge-base A B` will output a commit | |
70 | which is reachable from both 'A' and 'B' through the parent relationship. | |
71 | ||
72 | For example, with this topology: | |
73 | ||
74 | o---o---o---B | |
75 | / | |
76 | ---o---1---o---o---o---A | |
77 | ||
78 | the merge base between 'A' and 'B' is '1'. | |
79 | ||
80 | Given three commits 'A', 'B' and 'C', `git merge-base A B C` will compute the | |
29b802aa | 81 | merge base between 'A' and a hypothetical commit 'M', which is a merge |
99f1c04b JH |
82 | between '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 | ||
90 | the result of `git merge-base A B C` is '1'. This is because the | |
91 | equivalent 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 | ||
100 | and the result of `git merge-base A M` is '1'. Commit '2' is also a | |
101 | common ancestor between 'A' and 'M', but '1' is a better common ancestor, | |
102 | because '2' is an ancestor of '1'. Hence, '2' is not a merge base. | |
103 | ||
57294824 VR |
104 | The result of `git merge-base --octopus A B C` is '2', because '2' is |
105 | the best common ancestor of all commits. | |
106 | ||
99f1c04b | 107 | When 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 |
116 | both '1' and '2' are merge-bases of A and B. Neither one is better than |
117 | the other (both are 'best' merge bases). When the `--all` option is not given, | |
99f1c04b | 118 | it is unspecified which best one is output. |
2cf565c5 | 119 | |
5907cda1 JH |
120 | A common idiom to check "fast-forward-ness" between two commits A |
121 | and B is (or at least used to be) to compute the merge base between | |
122 | A and B, and check if it is the same as A, in which case, A is an | |
123 | ancestor 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 | ||
131 | In 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 | ||
138 | instead. | |
139 | ||
140 | ||
1846e9ed JN |
141 | See also |
142 | -------- | |
143 | linkgit:git-rev-list[1], | |
144 | linkgit:git-show-branch[1], | |
145 | linkgit:git-merge[1] | |
146 | ||
2cf565c5 DG |
147 | GIT |
148 | --- | |
9e1f0a85 | 149 | Part of the linkgit:git[1] suite |