Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 1 | git-merge-base(1) |
| 2 | ================= |
| 3 | |
| 4 | NAME |
| 5 | ---- |
Junio C Hamano | 7c73c66 | 2007-01-19 00:37:50 | [diff] [blame] | 6 | git-merge-base - Find as good common ancestors as possible for a merge |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 7 | |
| 8 | |
| 9 | SYNOPSIS |
| 10 | -------- |
Junio C Hamano | 2db3e75 | 2010-09-03 21:33:06 | [diff] [blame] | 11 | [verse] |
Junio C Hamano | b77f819 | 2011-05-05 01:30:38 | [diff] [blame] | 12 | 'git merge-base' [-a|--all] <commit> <commit>... |
| 13 | 'git merge-base' [-a|--all] --octopus <commit>... |
Junio C Hamano | 693e709 | 2012-09-12 22:56:53 | [diff] [blame] | 14 | 'git merge-base' --is-ancestor <commit> <commit> |
Junio C Hamano | 2db3e75 | 2010-09-03 21:33:06 | [diff] [blame] | 15 | 'git merge-base' --independent <commit>... |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 16 | |
| 17 | DESCRIPTION |
| 18 | ----------- |
Junio C Hamano | 6959c6c | 2006-05-17 10:34:11 | [diff] [blame] | 19 | |
Junio C Hamano | fd9274d | 2009-08-05 21:21:39 | [diff] [blame] | 20 | 'git merge-base' finds best common ancestor(s) between two commits to use |
Junio C Hamano | 3f2f6e5 | 2008-08-21 10:34:22 | [diff] [blame] | 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 |
Junio C Hamano | ec87f52 | 2008-12-10 08:35:25 | [diff] [blame] | 23 | that does not have any better common ancestor is a 'best common |
Junio C Hamano | 3f2f6e5 | 2008-08-21 10:34:22 | [diff] [blame] | 24 | ancestor', i.e. a 'merge base'. Note that there can be more than one |
Junio C Hamano | ec87f52 | 2008-12-10 08:35:25 | [diff] [blame] | 25 | merge base for a pair of commits. |
Junio C Hamano | 6959c6c | 2006-05-17 10:34:11 | [diff] [blame] | 26 | |
Junio C Hamano | b77f819 | 2011-05-05 01:30:38 | [diff] [blame] | 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. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 37 | |
Junio C Hamano | fd9274d | 2009-08-05 21:21:39 | [diff] [blame] | 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 | |
Junio C Hamano | 2db3e75 | 2010-09-03 21:33:06 | [diff] [blame] | 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 | |
Junio C Hamano | 693e709 | 2012-09-12 22:56:53 | [diff] [blame] | 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 | |
Junio C Hamano | b77f819 | 2011-05-05 01:30:38 | [diff] [blame] | 60 | OPTIONS |
| 61 | ------- |
| 62 | -a:: |
| 63 | --all:: |
| 64 | Output all merge bases for the commits, instead of just one. |
| 65 | |
Junio C Hamano | 3f2f6e5 | 2008-08-21 10:34:22 | [diff] [blame] | 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 |
Junio C Hamano | ec87f52 | 2008-12-10 08:35:25 | [diff] [blame] | 81 | merge base between 'A' and a hypothetical commit 'M', which is a merge |
Junio C Hamano | 3f2f6e5 | 2008-08-21 10:34:22 | [diff] [blame] | 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 | |
Junio C Hamano | b77f819 | 2011-05-05 01:30:38 | [diff] [blame] | 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 | |
Junio C Hamano | 3f2f6e5 | 2008-08-21 10:34:22 | [diff] [blame] | 107 | When the history involves criss-cross merges, there can be more than one |
Junio C Hamano | ec87f52 | 2008-12-10 08:35:25 | [diff] [blame] | 108 | 'best' common ancestor for two commits. For example, with this topology: |
Junio C Hamano | 3f2f6e5 | 2008-08-21 10:34:22 | [diff] [blame] | 109 | |
| 110 | ---1---o---A |
| 111 | \ / |
| 112 | X |
| 113 | / \ |
| 114 | ---2---o---o---B |
| 115 | |
Junio C Hamano | ec87f52 | 2008-12-10 08:35:25 | [diff] [blame] | 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, |
Junio C Hamano | 3f2f6e5 | 2008-08-21 10:34:22 | [diff] [blame] | 118 | it is unspecified which best one is output. |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 119 | |
Junio C Hamano | 693e709 | 2012-09-12 22:56:53 | [diff] [blame] | 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 | |
Junio C Hamano | 2db3e75 | 2010-09-03 21:33:06 | [diff] [blame] | 141 | See also |
| 142 | -------- |
| 143 | linkgit:git-rev-list[1], |
| 144 | linkgit:git-show-branch[1], |
| 145 | linkgit:git-merge[1] |
| 146 | |
Junio C Hamano | 1a4e841 | 2005-12-27 08:17:23 | [diff] [blame] | 147 | GIT |
| 148 | --- |
Junio C Hamano | f7c042d | 2008-06-06 22:50:53 | [diff] [blame] | 149 | Part of the linkgit:git[1] suite |