1git-merge-base(1) 2================= 3 4NAME 5---- 6git-merge-base - Find as good common ancestors as possible for a merge 7 8 9SYNOPSIS 10-------- 11[verse] 12'git merge-base' [-a|--all] <commit> <commit>... 13'git merge-base' [-a|--all] --octopus <commit>... 14'git merge-base' --is-ancestor <commit> <commit> 15'git merge-base' --independent <commit>... 16 17DESCRIPTION 18----------- 19 20'git merge-base' finds best common ancestor(s) between two commits to use 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 23that does not have any better common ancestor is a 'best common 24ancestor', i.e. a 'merge base'. Note that there can be more than one 25merge base for a pair of commits. 26 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. 37 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 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 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 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 60OPTIONS 61------- 62-a:: 63--all:: 64 Output all merge bases for the commits, instead of just one. 65 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 81merge base between 'A' and a hypothetical commit 'M', which is a merge 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 104The result of `git merge-base --octopus A B C` is '2', because '2' is 105the best common ancestor of all commits. 106 107When the history involves criss-cross merges, there can be more than one 108'best' common ancestor for two commits. For example, with this topology: 109 110 ---1---o---A 111 \ / 112 X 113 / \ 114 ---2---o---o---B 115 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, 118it is unspecified which best one is output. 119 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 141See also 142-------- 143linkgit:git-rev-list[1], 144linkgit:git-show-branch[1], 145linkgit:git-merge[1] 146 147GIT 148--- 149Part of the linkgit:git[1] suite