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 rerere-autoupdate update index with any reused conflict resolution
20 s,strategy= merge strategy to use
21 X= option for selected merge strategy
22 m,message= message to be used for the merge commit (if any)
30 test -z "$(git ls-files -u)" ||
31 die
"Merge is not possible because you have unmerged files."
33 ! test -e "$GIT_DIR/MERGE_HEAD" ||
34 die
'You have not concluded your merge (MERGE_HEAD exists).'
39 all_strategies
='recur recursive octopus resolve stupid ours subtree'
40 all_strategies
="$all_strategies recursive-ours recursive-theirs"
41 not_strategies
='base file index tree'
42 default_twohead_strategies
='recursive'
43 default_octopus_strategies
='octopus'
44 no_fast_forward_strategies
='subtree ours'
45 no_trivial_strategies
='recursive recur subtree ours recursive-ours recursive-theirs'
52 squash
= no_commit
= log_arg
= rr_arg
=
55 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
56 "$GIT_DIR/MERGE_STASH" "$GIT_DIR/MERGE_MODE" ||
exit 1
60 # Stash away any local modifications.
61 git stash create
>"$GIT_DIR/MERGE_STASH"
65 if test -f "$GIT_DIR/MERGE_STASH"
67 git
reset --hard $head >/dev
/null
68 git stash apply $
(cat "$GIT_DIR/MERGE_STASH")
69 git update-index
--refresh >/dev
/null
73 finish_up_to_date
() {
76 echo "$1 (nothing to squash)" ;;
84 echo Squashed commit of the following
:
86 git log
--no-merges --pretty=medium ^
"$head" $remoteheads
92 rlogm
="$GIT_REFLOG_ACTION"
95 rlogm
="$GIT_REFLOG_ACTION: $2"
99 echo "Squash commit -- not updating HEAD"
100 squash_message
>"$GIT_DIR/SQUASH_MSG"
105 echo "No merge message -- not updating HEAD"
108 git update-ref
-m "$rlogm" HEAD
"$1" "$head" ||
exit 1
118 if test "$show_diffstat" = t
120 # We want color (if set), but no pager
121 GIT_PAGER
='' git
diff --stat --summary -M "$head" "$1"
126 # Run a post-merge hook
127 if test -x "$GIT_DIR"/hooks
/post-merge
131 "$GIT_DIR"/hooks
/post-merge
1
134 "$GIT_DIR"/hooks
/post-merge
0
142 rh
=$
(git rev-parse
--verify "$remote^0" 2>/dev
/null
) ||
return
143 if truname
=$
(expr "$remote" : '\(.*\)~[0-9]*$') &&
144 git show-ref
-q --verify "refs/heads/$truname" 2>/dev
/null
146 echo "$rh branch '$truname' (early part) of ."
149 if found_ref
=$
(git rev-parse
--symbolic-full-name --verify \
150 "$remote" 2>/dev
/null
)
152 expanded
=$
(git check-ref-format
--branch "$remote") ||
154 if test "${found_ref#refs/heads/}" != "$found_ref"
156 echo "$rh branch '$expanded' of ."
158 elif test "${found_ref#refs/remotes/}" != "$found_ref"
160 echo "$rh remote branch '$expanded' of ."
164 if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
166 sed -e 's/ not-for-merge / /' -e 1q \
167 "$GIT_DIR/FETCH_HEAD"
170 echo "$rh commit '$remote'"
174 while test $# != 0; do
176 -n|
--no-stat|
--no-summary)
177 show_diffstat
=false
;;
183 test "$allow_fast_forward" = t ||
184 die
"You cannot combine --squash with --no-ff."
185 squash
=t no_commit
=t
;;
187 squash
= no_commit
= ;;
193 allow_fast_forward
=t
;;
195 test "$squash" != t ||
196 die
"You cannot combine --squash with --no-ff."
197 test "$fast_forward_only" != t ||
198 die
"You cannot combine --ff-only with --no-ff."
199 allow_fast_forward
=f
;;
201 test "$allow_fast_forward" != f ||
202 die
"You cannot combine --ff-only with --no-ff."
203 fast_forward_only
=t
;;
204 --rerere-autoupdate|
--no-rerere-autoupdate)
208 case " $all_strategies " in
210 use_strategies
="$use_strategies$1 "
213 case " $not_strategies " in
217 type "git-merge-$1" >/dev
/null
2>&1 ||
218 die
"available strategies are: $all_strategies"
219 use_strategies
="$use_strategies$1 "
225 xopt
="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")"
242 test $# != 0 || usage
246 if branch
=$
(git-symbolic-ref
-q HEAD
)
248 mergeopts
=$
(git config
"branch.${branch#refs/heads/}.mergeoptions")
249 if test -n "$mergeopts"
251 parse_config
$mergeopts --
256 while test $args_left -lt $#; do shift; done
258 if test -z "$show_diffstat"; then
259 test "$(git config --bool merge.diffstat)" = false
&& show_diffstat
=false
260 test "$(git config --bool merge.stat)" = false
&& show_diffstat
=false
261 test -z "$show_diffstat" && show_diffstat
=t
264 # This could be traditional "merge <msg> HEAD <commit>..." and the
265 # way we can tell it is to see if the second token is HEAD, but some
266 # people might have misused the interface and used a committish that
267 # is the same as HEAD there instead. Traditional format never would
268 # have "-m" so it is an additional safety measure to check for it.
270 if test -z "$have_message" &&
271 second_token
=$
(git rev-parse
--verify "$2^0" 2>/dev
/null
) &&
272 head_commit
=$
(git rev-parse
--verify "HEAD" 2>/dev
/null
) &&
273 test "$second_token" = "$head_commit"
279 elif ! git rev-parse
--verify HEAD
>/dev
/null
2>&1
281 # If the merged head is a valid one there is no reason to
282 # forbid "git merge" into a branch yet to be born. We do
283 # the same for "git pull".
286 echo >&2 "Can merge only exactly one commit into empty head"
290 test "$squash" != t ||
291 die
"Squash commit into empty head not supported yet"
292 test "$allow_fast_forward" = t ||
293 die
"Non-fast-forward into an empty head does not make sense"
294 rh
=$
(git rev-parse
--verify "$1^0") ||
295 die
"$1 - not something we can merge"
297 git update-ref
-m "initial pull" HEAD
"$rh" "" &&
298 git read-tree
--reset -u HEAD
302 # We are invoked directly as the first-class UI.
305 # All the rest are the commits being merged; prepare
306 # the standard merge summary message to be appended to
307 # the given message. If remote is invalid we will die
308 # later in the common codepath so we discard the error
315 if test "$have_message" = t
317 git fmt-merge-msg -m "$merge_msg" $log_arg
319 git fmt-merge-msg $log_arg
323 head=$
(git rev-parse
--verify "$head_arg"^
0) || usage
325 # All the rest are remote heads
326 test "$#" = 0 && usage
;# we need at least one remote head.
327 set_reflog_action
"merge $*"
332 remotehead
=$
(git rev-parse
--verify "$remote"^
0 2>/dev
/null
) ||
333 die
"$remote - not something we can merge"
334 remoteheads
="${remoteheads}$remotehead "
335 eval GITHEAD_
$remotehead='"$remote"'
336 export GITHEAD_
$remotehead
338 set x
$remoteheads ; shift
340 case "$use_strategies" in
344 var
="`git config --get pull.twohead`"
347 use_strategies
="$var"
349 use_strategies
="$default_twohead_strategies"
352 var
="`git config --get pull.octopus`"
355 use_strategies
="$var"
357 use_strategies
="$default_octopus_strategies"
363 for s
in $use_strategies
365 for ss
in $no_fast_forward_strategies
374 for ss
in $no_trivial_strategies
378 allow_trivial_merge
=f
387 common
=$
(git merge-base
--all $head "$@")
390 common
=$
(git merge-base
--all --octopus $head "$@")
393 echo "$head" >"$GIT_DIR/ORIG_HEAD"
395 case "$allow_fast_forward,$#,$common,$no_commit" in
397 # No common ancestors found. We need a real merge.
400 # If head can reach all the merge then we are up to date.
401 # but first the most common case of merging one remote.
402 finish_up_to_date
"Already up-to-date."
406 # Again the most common case of merging one remote.
407 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
408 git update-index
--refresh 2>/dev
/null
410 if test -n "$have_message"
412 msg
="$msg (no commit created; -m option ignored)"
414 new_head
=$
(git rev-parse
--verify "$1^0") &&
415 git read-tree
-v -m -u --exclude-per-directory=.gitignore
$head "$new_head" &&
416 finish
"$new_head" "$msg" ||
exit
421 # We are not doing octopus and not fast-forward. Need a
425 # We are not doing octopus, not fast-forward, and have only
427 git update-index
--refresh 2>/dev
/null
428 case "$allow_trivial_merge,$fast_forward_only" in
430 # See if it is really trivial.
431 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
432 echo "Trying really trivial in-index merge..."
433 if git read-tree
--trivial -m -u -v $common $head "$1" &&
434 result_tree
=$
(git write-tree
)
438 printf '%s\n' "$merge_msg" |
439 git commit-tree
$result_tree -p HEAD
-p "$1"
441 finish
"$result_commit" "In-index merge"
449 # An octopus. If we can reach all the remote we are up to date.
453 common_one
=$
(git merge-base
--all $head $remote)
454 if test "$common_one" != "$remote"
460 if test "$up_to_date" = t
462 finish_up_to_date
"Already up-to-date. Yeeah!"
468 if test "$fast_forward_only" = t
470 die
"Not possible to fast-forward, aborting."
473 # We are going to make a new commit.
474 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
476 # At this point, we need a real merge. No matter what strategy
477 # we use, it would operate on the index, possibly affecting the
478 # working tree, and when resolved cleanly, have the desired tree
479 # in the index -- this means that the index must be in sync with
480 # the $head commit. The strategies are responsible to ensure this.
482 case "$use_strategies" in
484 # Stash away the local changes so that we can try more than one.
489 rm -f "$GIT_DIR/MERGE_STASH"
494 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
496 for strategy
in $use_strategies
498 test "$wt_strategy" = '' ||
{
499 echo "Rewinding the tree to pristine..."
502 case "$single_strategy" in
504 echo "Trying merge strategy $strategy..."
508 # Remember which strategy left the state in the working tree
509 wt_strategy
=$strategy
511 eval 'git-merge-$strategy '"$xopt"' $common -- "$head_arg" "$@"'
513 if test "$no_commit" = t
&& test "$exit" = 0
516 exit=1 ;# pretend it left conflicts.
519 test "$exit" = 0 ||
{
521 # The backend exits with 1 when conflicts are left to be resolved,
522 # with 2 when it does not handle the given merge at all.
524 if test "$exit" -eq 1
527 git diff-files --name-only
528 git ls-files --unmerged
530 if test $best_cnt -le 0 -o $cnt -le $best_cnt
532 best_strategy
=$strategy
539 # Automerge succeeded.
540 result_tree
=$
(git write-tree
) && break
543 # If we have a resulting tree, that means the strategy module
544 # auto resolved the merge cleanly.
545 if test '' != "$result_tree"
547 if test "$allow_fast_forward" = "t"
549 parents
=$
(git merge-base
--independent "$head" "$@")
551 parents
=$
(git rev-parse
"$head" "$@")
553 parents
=$
(echo "$parents" |
sed -e 's/^/-p /')
554 result_commit
=$
(printf '%s\n' "$merge_msg" | git commit-tree
$result_tree $parents) ||
exit
555 finish
"$result_commit" "Merge made by $wt_strategy."
560 # Pick the result from the best strategy and have the user fix it up.
561 case "$best_strategy" in
564 case "$use_strategies" in
566 echo >&2 "No merge strategy handled the merge."
569 echo >&2 "Merge with strategy $use_strategies failed."
575 # We already have its result in the working tree.
578 echo "Rewinding the tree to pristine..."
580 echo "Using the $best_strategy to prepare resolving by hand."
581 git-merge-
$best_strategy $common -- "$head_arg" "$@"
585 if test "$squash" = t
592 done >"$GIT_DIR/MERGE_HEAD"
593 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG" ||
594 die
"Could not write to $GIT_DIR/MERGE_MSG"
595 if test "$allow_fast_forward" != t
600 fi >"$GIT_DIR/MERGE_MODE" ||
601 die
"Could not write to $GIT_DIR/MERGE_MODE"
604 if test "$merge_was_ok" = t
607 "Automatic merge went well; stopped before committing as requested"
614 git ls-files
--unmerged |
615 sed -e 's/^[^ ]* / /' |
617 } >>"$GIT_DIR/MERGE_MSG"
619 die
"Automatic merge failed; fix conflicts and then commit the result."