Makefile: make script-related rules usable from subdirectories
[git.git] / Documentation / git-merge-base.txt
blob87842e33f8aebc7d27fa732af1de15f0e4175c07
1 git-merge-base(1)
2 =================
4 NAME
5 ----
6 git-merge-base - Find as good common ancestors as possible for a merge
9 SYNOPSIS
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>...
17 DESCRIPTION
18 -----------
20 'git merge-base' finds best common ancestor(s) between two commits to use
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
23 that does not have any better common ancestor is a 'best common
24 ancestor', i.e. a 'merge base'.  Note that there can be more than one
25 merge base for a pair of commits.
27 OPERATION MODE
28 --------------
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.
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.
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.
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'.
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'.
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.
60 OPTIONS
61 -------
62 -a::
63 --all::
64         Output all merge bases for the commits, instead of just one.
66 DISCUSSION
67 ----------
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.
72 For example, with this topology:
74                  o---o---o---B
75                 /
76         ---o---1---o---o---o---A
78 the merge base between 'A' and 'B' is '1'.
80 Given three commits 'A', 'B' and 'C', `git merge-base A B C` will compute the
81 merge base between 'A' and a hypothetical commit 'M', which is a merge
82 between 'B' and 'C'.  For example, with this topology:
84                o---o---o---o---C
85               /
86              /   o---o---o---B
87             /   /
88         ---2---1---o---o---o---A
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:
94                o---o---o---o---o
95               /                 \
96              /   o---o---o---o---M
97             /   /
98         ---2---1---o---o---o---A
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.
104 The result of `git merge-base --octopus A B C` is '2', because '2' is
105 the best common ancestor of all commits.
107 When the history involves criss-cross merges, there can be more than one
108 'best' common ancestor for two commits.  For example, with this topology:
110        ---1---o---A
111            \ /
112             X
113            / \
114        ---2---o---o---B
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,
118 it is unspecified which best one is output.
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.
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
131 In modern git, you can say this in a more direct way:
133         if git merge-base --is-ancestor A B
134         then
135                 ... A is an ancestor of B ...
136         fi
138 instead.
141 See also
142 --------
143 linkgit:git-rev-list[1],
144 linkgit:git-show-branch[1],
145 linkgit:git-merge[1]
149 Part of the linkgit:git[1] suite