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 s,strategy= merge strategy to use
19 m,message= message to be used for the merge commit (if any)
27 test -z "$(git ls-files -u)" ||
28 die "Merge is not possible because you have unmerged files."
30 ! test -e "$GIT_DIR/MERGE_HEAD" ||
31 die 'You have not concluded your merge (MERGE_HEAD exists).'
36 all_strategies='recur recursive octopus resolve stupid ours subtree'
37 all_strategies="$all_strategies recursive-ours recursive-theirs"
38 default_twohead_strategies='recursive'
39 default_octopus_strategies='octopus'
40 no_fast_forward_strategies='subtree ours'
41 no_trivial_strategies='recursive recur subtree ours recursive-ours recursive-theirs'
46 squash= no_commit= log_arg=
49 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
50 "$GIT_DIR/MERGE_STASH" || exit 1
54 # Stash away any local modifications.
55 git stash create >"$GIT_DIR/MERGE_STASH"
59 if test -f "$GIT_DIR/MERGE_STASH"
61 git reset --hard $head >/dev/null
62 git stash apply $(cat "$GIT_DIR/MERGE_STASH")
63 git update-index --refresh >/dev/null
67 finish_up_to_date () {
70 echo "$1 (nothing to squash)" ;;
78 echo Squashed commit of the following:
80 git log --no-merges --pretty=medium ^"$head" $remoteheads
86 rlogm="$GIT_REFLOG_ACTION"
89 rlogm="$GIT_REFLOG_ACTION: $2"
93 echo "Squash commit -- not updating HEAD"
94 squash_message >"$GIT_DIR/SQUASH_MSG"
99 echo "No merge message -- not updating HEAD"
102 git update-ref -m "$rlogm" HEAD "$1" "$head" || exit 1
112 if test "$show_diffstat" = t
114 # We want color (if set), but no pager
115 GIT_PAGER='' git diff --stat --summary -M "$head" "$1"
120 # Run a post-merge hook
121 if test -x "$GIT_DIR"/hooks/post-merge
125 "$GIT_DIR"/hooks/post-merge 1
128 "$GIT_DIR"/hooks/post-merge 0
136 rh=$(git rev-parse --verify "$remote^0" 2>/dev/null) || return
137 if truname=$(expr "$remote" : '\(.*\)~[0-9]*$') &&
138 git show-ref -q --verify "refs/heads/$truname" 2>/dev/null
140 echo "$rh branch '$truname' (early part) of ."
143 if found_ref=$(git rev-parse --symbolic-full-name --verify \
144 "$remote" 2>/dev/null)
146 if test "${found_ref#refs/heads/}" != "$found_ref"
148 echo "$rh branch '$remote' of ."
150 elif test "${found_ref#refs/remotes/}" != "$found_ref"
152 echo "$rh remote branch '$remote' of ."
156 if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
158 sed -e 's/ not-for-merge / /' -e 1q \
159 "$GIT_DIR/FETCH_HEAD"
162 echo "$rh commit '$remote'"
166 while test $# != 0; do
168 -n|--no-stat|--no-summary)
169 show_diffstat=false ;;
175 test "$allow_fast_forward" = t ||
176 die "You cannot combine --squash with --no-ff."
177 squash=t no_commit=t ;;
179 squash= no_commit= ;;
185 allow_fast_forward=t ;;
187 test "$squash" != t ||
188 die "You cannot combine --squash with --no-ff."
189 allow_fast_forward=f ;;
192 case " $all_strategies " in
194 use_strategies="$use_strategies$1 " ;;
196 die "available strategies are: $all_strategies" ;;
214 test $# != 0 || usage
218 if branch=$(git-symbolic-ref -q HEAD)
220 mergeopts=$(git config "branch.${branch#refs/heads/}.mergeoptions")
221 if test -n "$mergeopts"
223 parse_config $mergeopts --
228 while test $args_left -lt $#; do shift; done
230 if test -z "$show_diffstat"; then
231 test "$(git config --bool merge.diffstat)" = false && show_diffstat=false
232 test "$(git config --bool merge.stat)" = false && show_diffstat=false
233 test -z "$show_diffstat" && show_diffstat=t
236 # This could be traditional "merge <msg> HEAD <commit>..." and the
237 # way we can tell it is to see if the second token is HEAD, but some
238 # people might have misused the interface and used a committish that
239 # is the same as HEAD there instead. Traditional format never would
240 # have "-m" so it is an additional safety measure to check for it.
242 if test -z "$have_message" &&
243 second_token=$(git rev-parse --verify "$2^0" 2>/dev/null) &&
244 head_commit=$(git rev-parse --verify "HEAD" 2>/dev/null) &&
245 test "$second_token" = "$head_commit"
251 elif ! git rev-parse --verify HEAD >/dev/null 2>&1
253 # If the merged head is a valid one there is no reason to
254 # forbid "git merge" into a branch yet to be born. We do
255 # the same for "git pull".
258 echo >&2 "Can merge only exactly one commit into empty head"
262 test "$squash" != t ||
263 die "Squash commit into empty head not supported yet"
264 test "$allow_fast_forward" = t ||
265 die "Non-fast-forward into an empty head does not make sense"
266 rh=$(git rev-parse --verify "$1^0") ||
267 die "$1 - not something we can merge"
269 git update-ref -m "initial pull" HEAD "$rh" "" &&
270 git read-tree --reset -u HEAD
274 # We are invoked directly as the first-class UI.
277 # All the rest are the commits being merged; prepare
278 # the standard merge summary message to be appended to
279 # the given message. If remote is invalid we will die
280 # later in the common codepath so we discard the error
282 merge_name=$(for remote
285 done | git fmt-merge-msg $log_arg
287 merge_msg="${merge_msg:+$merge_msg$LF$LF}$merge_name"
289 head=$(git rev-parse --verify "$head_arg"^0) || usage
291 # All the rest are remote heads
292 test "$#" = 0 && usage ;# we need at least one remote head.
293 set_reflog_action "merge $*"
298 remotehead=$(git rev-parse --verify "$remote"^0 2>/dev/null) ||
299 die "$remote - not something we can merge"
300 remoteheads="${remoteheads}$remotehead "
301 eval GITHEAD_$remotehead='"$remote"'
302 export GITHEAD_$remotehead
304 set x $remoteheads ; shift
306 case "$use_strategies" in
310 var="`git config --get pull.twohead`"
313 use_strategies="$var"
315 use_strategies="$default_twohead_strategies"
318 var="`git config --get pull.octopus`"
321 use_strategies="$var"
323 use_strategies="$default_octopus_strategies"
329 for s in $use_strategies
331 for ss in $no_fast_forward_strategies
340 for ss in $no_trivial_strategies
344 allow_trivial_merge=f
353 common=$(git merge-base --all $head "$@")
356 common=$(git show-branch --merge-base $head "$@")
359 echo "$head" >"$GIT_DIR/ORIG_HEAD"
361 case "$allow_fast_forward,$#,$common,$no_commit" in
363 # No common ancestors found. We need a real merge.
366 # If head can reach all the merge then we are up to date.
367 # but first the most common case of merging one remote.
368 finish_up_to_date "Already up-to-date."
372 # Again the most common case of merging one remote.
373 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
374 git update-index --refresh 2>/dev/null
376 if test -n "$have_message"
378 msg="$msg (no commit created; -m option ignored)"
380 new_head=$(git rev-parse --verify "$1^0") &&
381 git read-tree -v -m -u --exclude-per-directory=.gitignore $head "$new_head" &&
382 finish "$new_head" "$msg" || exit
387 # We are not doing octopus and not fast-forward. Need a
391 # We are not doing octopus, not fast-forward, and have only
393 git update-index --refresh 2>/dev/null
394 case "$allow_trivial_merge" in
396 # See if it is really trivial.
397 git var GIT_COMMITTER_IDENT >/dev/null || exit
398 echo "Trying really trivial in-index merge..."
399 if git read-tree --trivial -m -u -v $common $head "$1" &&
400 result_tree=$(git write-tree)
404 printf '%s\n' "$merge_msg" |
405 git commit-tree $result_tree -p HEAD -p "$1"
407 finish "$result_commit" "In-index merge"
415 # An octopus. If we can reach all the remote we are up to date.
419 common_one=$(git merge-base --all $head $remote)
420 if test "$common_one" != "$remote"
426 if test "$up_to_date" = t
428 finish_up_to_date "Already up-to-date. Yeeah!"
434 # We are going to make a new commit.
435 git var GIT_COMMITTER_IDENT >/dev/null || exit
437 # At this point, we need a real merge. No matter what strategy
438 # we use, it would operate on the index, possibly affecting the
439 # working tree, and when resolved cleanly, have the desired tree
440 # in the index -- this means that the index must be in sync with
441 # the $head commit. The strategies are responsible to ensure this.
443 case "$use_strategies" in
445 # Stash away the local changes so that we can try more than one.
450 rm -f "$GIT_DIR/MERGE_STASH"
455 result_tree= best_cnt=-1 best_strategy= wt_strategy=
457 for strategy in $use_strategies
459 test "$wt_strategy" = '' || {
460 echo "Rewinding the tree to pristine..."
463 case "$single_strategy" in
465 echo "Trying merge strategy $strategy..."
469 # Remember which strategy left the state in the working tree
470 wt_strategy=$strategy
472 git-merge-$strategy $common -- "$head_arg" "$@"
474 if test "$no_commit" = t && test "$exit" = 0
477 exit=1 ;# pretend it left conflicts.
480 test "$exit" = 0 || {
482 # The backend exits with 1 when conflicts are left to be resolved,
483 # with 2 when it does not handle the given merge at all.
485 if test "$exit" -eq 1
488 git diff-files --name-only
489 git ls-files --unmerged
491 if test $best_cnt -le 0 -o $cnt -le $best_cnt
493 best_strategy=$strategy
500 # Automerge succeeded.
501 result_tree=$(git write-tree) && break
504 # If we have a resulting tree, that means the strategy module
505 # auto resolved the merge cleanly.
506 if test '' != "$result_tree"
508 if test "$allow_fast_forward" = "t"
510 parents=$(git show-branch --independent "$head" "$@")
512 parents=$(git rev-parse "$head" "$@")
514 parents=$(echo "$parents" | sed -e 's/^/-p /')
515 result_commit=$(printf '%s\n' "$merge_msg" | git commit-tree $result_tree $parents) || exit
516 finish "$result_commit" "Merge made by $wt_strategy."
521 # Pick the result from the best strategy and have the user fix it up.
522 case "$best_strategy" in
525 case "$use_strategies" in
527 echo >&2 "No merge strategy handled the merge."
530 echo >&2 "Merge with strategy $use_strategies failed."
536 # We already have its result in the working tree.
539 echo "Rewinding the tree to pristine..."
541 echo "Using the $best_strategy to prepare resolving by hand."
542 git-merge-$best_strategy $common -- "$head_arg" "$@"
546 if test "$squash" = t
553 done >"$GIT_DIR/MERGE_HEAD"
554 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
557 if test "$merge_was_ok" = t
560 "Automatic merge went well; stopped before committing as requested"
567 git ls-files --unmerged |
568 sed -e 's/^[^ ]* / /' |
570 } >>"$GIT_DIR/MERGE_MSG"
572 die "Automatic merge failed; fix conflicts and then commit the result."