3 # Copyright (c) 2005 Junio C Hamano
8 git merge [options] <remote>...
9 git merge [options] <msg> HEAD <remote>
11 stat show a diffstat at the end of the merge
12 n don't show a diffstat at the end of the merge
13 summary (synonym to --stat)
14 log add list of one-line log to merge commit message
15 squash create a single commit instead of doing a merge
16 commit perform a commit if the merge succeeds (default)
17 ff allow fast-forward (default)
18 ff-only abort if fast-forward is not possible
19 s,strategy= merge strategy to use
20 X= option for selected merge strategy
21 m,message= message to be used for the merge commit (if any)
29 test -z "$(git ls-files -u)" ||
30 die "Merge is not possible because you have unmerged files."
32 ! test -e "$GIT_DIR/MERGE_HEAD" ||
33 die 'You have not concluded your merge (MERGE_HEAD exists).'
38 all_strategies='recur recursive octopus resolve stupid ours subtree'
39 all_strategies="$all_strategies recursive-ours recursive-theirs"
40 not_strategies='base file index tree'
41 default_twohead_strategies='recursive'
42 default_octopus_strategies='octopus'
43 no_fast_forward_strategies='subtree ours'
44 no_trivial_strategies='recursive recur subtree ours recursive-ours recursive-theirs'
51 squash= no_commit= log_arg=
54 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
55 "$GIT_DIR/MERGE_STASH" "$GIT_DIR/MERGE_MODE" || exit 1
59 # Stash away any local modifications.
60 git stash create >"$GIT_DIR/MERGE_STASH"
64 if test -f "$GIT_DIR/MERGE_STASH"
66 git reset --hard $head >/dev/null
67 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
68 git update-index --refresh >/dev/null
72 finish_up_to_date () {
75 echo "$1 (nothing to squash)" ;;
83 echo Squashed commit of the following:
85 git log --no-merges --pretty=medium ^"$head" $remoteheads
91 rlogm="$GIT_REFLOG_ACTION"
94 rlogm="$GIT_REFLOG_ACTION: $2"
98 echo "Squash commit -- not updating HEAD"
99 squash_message >"$GIT_DIR/SQUASH_MSG"
104 echo "No merge message -- not updating HEAD"
107 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
117 if test "$show_diffstat" = t
119 # We want color (if set), but no pager
120 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
125 # Run a post-merge hook
126 if test -x "$GIT_DIR"/hooks/post-merge
130 "$GIT_DIR"/hooks/post-merge 1
133 "$GIT_DIR"/hooks/post-merge 0
141 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
142 if truname=$(expr "$remote" : '\(.*\)~[0-9]*$') &&
143 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
145 echo "$rh branch '$truname' (early part) of ."
148 if found_ref=$(git rev-parse --symbolic-full-name --verify \
149 "$remote" 2>/dev/null)
151 expanded=$(git check-ref-format --branch "$remote") ||
153 if test "${found_ref#refs/heads/}" != "$found_ref"
155 echo "$rh branch '$expanded' of ."
157 elif test "${found_ref#refs/remotes/}" != "$found_ref"
159 echo "$rh remote branch '$expanded' of ."
163 if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
165 sed -e 's/ not-for-merge / /' -e 1q \
166 "$GIT_DIR/FETCH_HEAD"
169 echo "$rh commit '$remote'"
173 while test $# != 0; do
175 -n|--no-stat|--no-summary)
176 show_diffstat=false ;;
182 test "$allow_fast_forward" = t ||
183 die "You cannot combine --squash with --no-ff."
184 squash=t no_commit=t ;;
186 squash= no_commit= ;;
192 allow_fast_forward=t ;;
194 test "$squash" != t ||
195 die "You cannot combine --squash with --no-ff."
196 test "$fast_forward_only" != t ||
197 die "You cannot combine --ff-only with --no-ff."
198 allow_fast_forward=f ;;
200 test "$allow_fast_forward" != f ||
201 die "You cannot combine --ff-only with --no-ff."
202 fast_forward_only=t ;;
205 case " $all_strategies " in
207 use_strategies="$use_strategies$1 "
210 case " $not_strategies " in
214 type "git-merge-$1" >/dev/null 2>&1 ||
215 die "available strategies are: $all_strategies"
216 use_strategies="$use_strategies$1 "
222 xopt="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")"
239 test $# != 0 || usage
243 if branch=$(git-symbolic-ref -q HEAD)
245 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
246 if test -n "$mergeopts"
248 parse_config $mergeopts --
253 while test $args_left -lt $#; do shift; done
255 if test -z "$show_diffstat"; then
256 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
257 test "$(git config --bool merge.stat)" = false && show_diffstat=false
258 test -z "$show_diffstat" && show_diffstat=t
261 # This could be traditional "merge <msg> HEAD <commit>..." and the
262 # way we can tell it is to see if the second token is HEAD, but some
263 # people might have misused the interface and used a committish that
264 # is the same as HEAD there instead. Traditional format never would
265 # have "-m" so it is an additional safety measure to check for it.
267 if test -z "$have_message" &&
268 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
269 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
270 test "$second_token" = "$head_commit"
276 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
278 # If the merged head is a valid one there is no reason to
279 # forbid "git merge" into a branch yet to be born. We do
280 # the same for "git pull".
283 echo >&2 "Can merge only exactly one commit into empty head"
287 test "$squash" != t ||
288 die "Squash commit into empty head not supported yet"
289 test "$allow_fast_forward" = t ||
290 die "Non-fast-forward into an empty head does not make sense"
291 rh=$(git rev-parse --verify "$1^0") ||
292 die "$1 - not something we can merge"
294 git update-ref -m "initial pull" HEAD "$rh" "" &&
295 git read-tree --reset -u HEAD
299 # We are invoked directly as the first-class UI.
302 # All the rest are the commits being merged; prepare
303 # the standard merge summary message to be appended to
304 # the given message. If remote is invalid we will die
305 # later in the common codepath so we discard the error
312 if test "$have_message" = t
314 git fmt-merge-msg -m "$merge_msg" $log_arg
316 git fmt-merge-msg $log_arg
320 head=$(git rev-parse --verify "$head_arg"^0) || usage
322 # All the rest are remote heads
323 test "$#" = 0 && usage ;# we need at least one remote head.
324 set_reflog_action "merge $*"
329 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
330 die "$remote - not something we can merge"
331 remoteheads="${remoteheads}$remotehead "
332 eval GITHEAD_$remotehead='"$remote"'
333 export GITHEAD_$remotehead
335 set x $remoteheads ; shift
337 case "$use_strategies" in
341 var="`git config --get pull.twohead`"
344 use_strategies="$var"
346 use_strategies="$default_twohead_strategies"
349 var="`git config --get pull.octopus`"
352 use_strategies="$var"
354 use_strategies="$default_octopus_strategies"
360 for s in $use_strategies
362 for ss in $no_fast_forward_strategies
371 for ss in $no_trivial_strategies
375 allow_trivial_merge=f
384 common=$(git merge-base --all $head "$@")
387 common=$(git merge-base --all --octopus $head "$@")
390 echo "$head" >"$GIT_DIR/ORIG_HEAD"
392 case "$allow_fast_forward,$#,$common,$no_commit" in
394 # No common ancestors found. We need a real merge.
397 # If head can reach all the merge then we are up to date.
398 # but first the most common case of merging one remote.
399 finish_up_to_date "Already up-to-date."
403 # Again the most common case of merging one remote.
404 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
405 git update-index --refresh 2>/dev/null
407 if test -n "$have_message"
409 msg="$msg (no commit created; -m option ignored)"
411 new_head=$(git rev-parse --verify "$1^0") &&
412 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
413 finish "$new_head" "$msg" || exit
418 # We are not doing octopus and not fast-forward. Need a
422 # We are not doing octopus, not fast-forward, and have only
424 git update-index --refresh 2>/dev/null
425 case "$allow_trivial_merge,$fast_forward_only" in
427 # See if it is really trivial.
428 git var GIT_COMMITTER_IDENT >/dev/null || exit
429 echo "Trying really trivial in-index merge..."
430 if git read-tree --trivial -m -u -v $common $head "$1" &&
431 result_tree=$(git write-tree)
435 printf '%s\n' "$merge_msg" |
436 git commit-tree $result_tree -p HEAD -p "$1"
438 finish "$result_commit" "In-index merge"
446 # An octopus. If we can reach all the remote we are up to date.
450 common_one=$(git merge-base --all $head $remote)
451 if test "$common_one" != "$remote"
457 if test "$up_to_date" = t
459 finish_up_to_date "Already up-to-date. Yeeah!"
465 if test "$fast_forward_only" = t
467 die "Not possible to fast-forward, aborting."
470 # We are going to make a new commit.
471 git var GIT_COMMITTER_IDENT >/dev/null || exit
473 # At this point, we need a real merge. No matter what strategy
474 # we use, it would operate on the index, possibly affecting the
475 # working tree, and when resolved cleanly, have the desired tree
476 # in the index -- this means that the index must be in sync with
477 # the $head commit. The strategies are responsible to ensure this.
479 case "$use_strategies" in
481 # Stash away the local changes so that we can try more than one.
486 rm -f "$GIT_DIR/MERGE_STASH"
491 result_tree= best_cnt=-1 best_strategy= wt_strategy=
493 for strategy in $use_strategies
495 test "$wt_strategy" = '' || {
496 echo "Rewinding the tree to pristine..."
499 case "$single_strategy" in
501 echo "Trying merge strategy $strategy..."
505 # Remember which strategy left the state in the working tree
506 wt_strategy=$strategy
508 eval 'git-merge-$strategy '"$xopt"' $common -- "$head_arg" "$@"'
510 if test "$no_commit" = t && test "$exit" = 0
513 exit=1 ;# pretend it left conflicts.
516 test "$exit" = 0 || {
518 # The backend exits with 1 when conflicts are left to be resolved,
519 # with 2 when it does not handle the given merge at all.
521 if test "$exit" -eq 1
524 git diff-files --name-only
525 git ls-files --unmerged
527 if test $best_cnt -le 0 -o $cnt -le $best_cnt
529 best_strategy=$strategy
536 # Automerge succeeded.
537 result_tree=$(git write-tree) && break
540 # If we have a resulting tree, that means the strategy module
541 # auto resolved the merge cleanly.
542 if test '' != "$result_tree"
544 if test "$allow_fast_forward" = "t"
546 parents=$(git merge-base --independent "$head" "$@")
548 parents=$(git rev-parse "$head" "$@")
550 parents=$(echo "$parents" | sed -e 's/^/-p /')
551 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
552 finish "$result_commit" "Merge made by $wt_strategy."
557 # Pick the result from the best strategy and have the user fix it up.
558 case "$best_strategy" in
561 case "$use_strategies" in
563 echo >&2 "No merge strategy handled the merge."
566 echo >&2 "Merge with strategy $use_strategies failed."
572 # We already have its result in the working tree.
575 echo "Rewinding the tree to pristine..."
577 echo "Using the $best_strategy to prepare resolving by hand."
578 git-merge-$best_strategy $common -- "$head_arg" "$@"
582 if test "$squash" = t
589 done >"$GIT_DIR/MERGE_HEAD"
590 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG" ||
591 die "Could not write to $GIT_DIR/MERGE_MSG"
592 if test "$allow_fast_forward" != t
597 fi >"$GIT_DIR/MERGE_MODE" ||
598 die "Could not write to $GIT_DIR/MERGE_MODE"
601 if test "$merge_was_ok" = t
604 "Automatic merge went well; stopped before committing as requested"
611 git ls-files --unmerged |
612 sed -e 's/^[^ ]* / /' |
614 } >>"$GIT_DIR/MERGE_MSG"
616 die "Automatic merge failed; fix conflicts and then commit the result."