3 # Copyright (c) 2005 Junio C Hamano
8 git-merge [options] <remote>...
9 git-merge [options] <msg> HEAD <remote>
11 summary show a diffstat at the end of the merge
12 n,no-summary don't show a diffstat at the end of the merge
13 squash create a single commit instead of doing a merge
14 commit perform a commit if the merge sucesses (default)
15 ff allow fast forward (default)
16 s,strategy= merge strategy to use
17 m,message= message to be used for the merge commit (if any)
25 test -z "$(git ls-files -u)" ||
26 die "You are in the middle of a conflicted merge."
31 all_strategies='recur recursive octopus resolve stupid ours subtree'
32 default_twohead_strategies='recursive'
33 default_octopus_strategies='octopus'
34 no_fast_forward_strategies='subtree ours'
35 no_trivial_strategies='recursive recur subtree ours'
42 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
43 "$GIT_DIR/MERGE_STASH" || exit 1
47 # Stash away any local modifications.
48 git stash create >"$GIT_DIR/MERGE_STASH"
52 if test -f "$GIT_DIR/MERGE_STASH"
54 git reset --hard $head >/dev/null
55 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
56 git update-index --refresh >/dev/null
60 finish_up_to_date () {
63 echo "$1 (nothing to squash)" ;;
71 echo Squashed commit of the following:
73 git log --no-merges ^"$head" $remoteheads
79 rlogm="$GIT_REFLOG_ACTION"
82 rlogm="$GIT_REFLOG_ACTION: $2"
86 echo "Squash commit -- not updating HEAD"
87 squash_message >"$GIT_DIR/SQUASH_MSG"
92 echo "No merge message -- not updating HEAD"
95 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
105 if test "$show_diffstat" = t
107 # We want color (if set), but no pager
108 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
113 # Run a post-merge hook
114 if test -x "$GIT_DIR"/hooks/post-merge
118 "$GIT_DIR"/hooks/post-merge 1
121 "$GIT_DIR"/hooks/post-merge 0
129 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
130 bh=$(git show-ref -s --verify "refs/heads/$remote" 2>/dev/null)
131 if test "$rh" = "$bh"
133 echo "$rh branch '$remote' of ."
134 elif truname=$(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
135 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
137 echo "$rh branch '$truname' (early part) of ."
138 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
140 sed -e 's/ not-for-merge / /' -e 1q \
141 "$GIT_DIR/FETCH_HEAD"
143 echo "$rh commit '$remote'"
148 while test $# != 0; do
151 show_diffstat=false ;;
155 allow_fast_forward=t squash=t no_commit=t ;;
157 allow_fast_forward=t squash= no_commit= ;;
159 allow_fast_forward=t squash= no_commit= ;;
161 allow_fast_forward=t squash= no_commit=t ;;
163 allow_fast_forward=t squash= no_commit= ;;
165 allow_fast_forward=false squash= no_commit= ;;
168 case " $all_strategies " in
170 use_strategies="$use_strategies$1 " ;;
172 die "available strategies are: $all_strategies" ;;
190 test $# != 0 || usage
194 if branch=$(git-symbolic-ref -q HEAD)
196 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
197 if test -n "$mergeopts"
199 parse_config $mergeopts --
204 while test $args_left -lt $#; do shift; done
206 if test -z "$show_diffstat"; then
207 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
208 test -z "$show_diffstat" && show_diffstat=t
211 # This could be traditional "merge <msg> HEAD <commit>..." and the
212 # way we can tell it is to see if the second token is HEAD, but some
213 # people might have misused the interface and used a committish that
214 # is the same as HEAD there instead. Traditional format never would
215 # have "-m" so it is an additional safety measure to check for it.
217 if test -z "$have_message" &&
218 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
219 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
220 test "$second_token" = "$head_commit"
226 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
228 # If the merged head is a valid one there is no reason to
229 # forbid "git merge" into a branch yet to be born. We do
230 # the same for "git pull".
233 echo >&2 "Can merge only exactly one commit into empty head"
237 rh=$(git rev-parse --verify "$1^0") ||
238 die "$1 - not something we can merge"
240 git update-ref -m "initial pull" HEAD "$rh" "" &&
241 git read-tree --reset -u HEAD
245 # We are invoked directly as the first-class UI.
248 # All the rest are the commits being merged; prepare
249 # the standard merge summary message to be appended to
250 # the given message. If remote is invalid we will die
251 # later in the common codepath so we discard the error
253 merge_name=$(for remote
256 done | git fmt-merge-msg
258 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
260 head=$(git rev-parse --verify "$head_arg"^0) || usage
262 # All the rest are remote heads
263 test "$#" = 0 && usage ;# we need at least one remote head.
264 set_reflog_action "merge $*"
269 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
270 die "$remote - not something we can merge"
271 remoteheads="${remoteheads}$remotehead "
272 eval GITHEAD_$remotehead='"$remote"'
273 export GITHEAD_$remotehead
275 set x $remoteheads ; shift
277 case "$use_strategies" in
281 var="`git config --get pull.twohead`"
284 use_strategies="$var"
286 use_strategies="$default_twohead_strategies"
289 var="`git config --get pull.octopus`"
292 use_strategies="$var"
294 use_strategies="$default_octopus_strategies"
300 for s in $use_strategies
302 for ss in $no_fast_forward_strategies
311 for ss in $no_trivial_strategies
315 allow_trivial_merge=f
324 common=$(git merge-base --all $head "$@")
327 common=$(git show-branch --merge-base $head "$@")
330 echo "$head" >"$GIT_DIR/ORIG_HEAD"
332 case "$allow_fast_forward,$#,$common,$no_commit" in
334 # No common ancestors found. We need a real merge.
337 # If head can reach all the merge then we are up to date.
338 # but first the most common case of merging one remote.
339 finish_up_to_date "Already up-to-date."
343 # Again the most common case of merging one remote.
344 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
345 git update-index --refresh 2>/dev/null
347 if test -n "$have_message"
349 msg="$msg (no commit created; -m option ignored)"
351 new_head=$(git rev-parse --verify "$1^0") &&
352 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
353 finish "$new_head" "$msg" || exit
358 # We are not doing octopus and not fast forward. Need a
362 # We are not doing octopus, not fast forward, and have only
364 git update-index --refresh 2>/dev/null
365 case "$allow_trivial_merge" in
367 # See if it is really trivial.
368 git var GIT_COMMITTER_IDENT >/dev/null || exit
369 echo "Trying really trivial in-index merge..."
370 if git read-tree --trivial -m -u -v $common $head "$1" &&
371 result_tree=$(git write-tree)
375 printf '%s\n' "$merge_msg" |
376 git commit-tree $result_tree -p HEAD -p "$1"
378 finish "$result_commit" "In-index merge"
386 # An octopus. If we can reach all the remote we are up to date.
390 common_one=$(git merge-base --all $head $remote)
391 if test "$common_one" != "$remote"
397 if test "$up_to_date" = t
399 finish_up_to_date "Already up-to-date. Yeeah!"
405 # We are going to make a new commit.
406 git var GIT_COMMITTER_IDENT >/dev/null || exit
408 # At this point, we need a real merge. No matter what strategy
409 # we use, it would operate on the index, possibly affecting the
410 # working tree, and when resolved cleanly, have the desired tree
411 # in the index -- this means that the index must be in sync with
412 # the $head commit. The strategies are responsible to ensure this.
414 case "$use_strategies" in
416 # Stash away the local changes so that we can try more than one.
421 rm -f "$GIT_DIR/MERGE_STASH"
426 result_tree= best_cnt=-1 best_strategy= wt_strategy=
428 for strategy in $use_strategies
430 test "$wt_strategy" = '' || {
431 echo "Rewinding the tree to pristine..."
434 case "$single_strategy" in
436 echo "Trying merge strategy $strategy..."
440 # Remember which strategy left the state in the working tree
441 wt_strategy=$strategy
443 git-merge-$strategy $common -- "$head_arg" "$@"
445 if test "$no_commit" = t && test "$exit" = 0
448 exit=1 ;# pretend it left conflicts.
451 test "$exit" = 0 || {
453 # The backend exits with 1 when conflicts are left to be resolved,
454 # with 2 when it does not handle the given merge at all.
456 if test "$exit" -eq 1
459 git diff-files --name-only
460 git ls-files --unmerged
462 if test $best_cnt -le 0 -o $cnt -le $best_cnt
464 best_strategy=$strategy
471 # Automerge succeeded.
472 result_tree=$(git write-tree) && break
475 # If we have a resulting tree, that means the strategy module
476 # auto resolved the merge cleanly.
477 if test '' != "$result_tree"
479 if test "$allow_fast_forward" = "t"
481 parents=$(git show-branch --independent "$head" "$@")
483 parents=$(git rev-parse "$head" "$@")
485 parents=$(echo "$parents" | sed -e 's/^/-p /')
486 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
487 finish "$result_commit" "Merge made by $wt_strategy."
492 # Pick the result from the best strategy and have the user fix it up.
493 case "$best_strategy" in
496 case "$use_strategies" in
498 echo >&2 "No merge strategy handled the merge."
501 echo >&2 "Merge with strategy $use_strategies failed."
507 # We already have its result in the working tree.
510 echo "Rewinding the tree to pristine..."
512 echo "Using the $best_strategy to prepare resolving by hand."
513 git-merge-$best_strategy $common -- "$head_arg" "$@"
517 if test "$squash" = t
524 done >"$GIT_DIR/MERGE_HEAD"
525 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
528 if test "$merge_was_ok" = t
531 "Automatic merge went well; stopped before committing as requested"
538 git ls-files --unmerged |
539 sed -e 's/^[^ ]* / /' |
541 } >>"$GIT_DIR/MERGE_MSG"
543 die "Automatic merge failed; fix conflicts and then commit the result."