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" ||
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 if test "${found_ref#refs/heads/}" != "$found_ref"
153 echo "$rh branch '$remote' of ."
155 elif test "${found_ref#refs/remotes/}" != "$found_ref"
157 echo "$rh remote branch '$remote' of ."
161 if test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
163 sed -e 's/ not-for-merge / /' -e 1q \
164 "$GIT_DIR/FETCH_HEAD"
167 echo "$rh commit '$remote'"
171 while test $# != 0; do
173 -n|
--no-stat|
--no-summary)
174 show_diffstat
=false
;;
180 test "$allow_fast_forward" = t ||
181 die
"You cannot combine --squash with --no-ff."
182 squash
=t no_commit
=t
;;
184 squash
= no_commit
= ;;
190 allow_fast_forward
=t
;;
192 test "$squash" != t ||
193 die
"You cannot combine --squash with --no-ff."
194 test "$fast_forward_only" != t ||
195 die
"You cannot combine --ff-only with --no-ff."
196 allow_fast_forward
=f
;;
198 test "$allow_fast_forward" != f ||
199 die
"You cannot combine --ff-only with --no-ff."
200 fast_forward_only
=t
;;
203 case " $all_strategies " in
205 use_strategies
="$use_strategies$1 "
208 case " $not_strategies " in
212 type "git-merge-$1" >/dev
/null
2>&1 ||
213 die
"available strategies are: $all_strategies"
214 use_strategies
="$use_strategies$1 "
220 xopt
="${xopt:+$xopt }$(git rev-parse --sq-quote "--$1")"
237 test $# != 0 || usage
241 if branch
=$
(git-symbolic-ref
-q HEAD
)
243 mergeopts
=$
(git config
"branch.${branch#refs/heads/}.mergeoptions")
244 if test -n "$mergeopts"
246 parse_config
$mergeopts --
251 while test $args_left -lt $#; do shift; done
253 if test -z "$show_diffstat"; then
254 test "$(git config --bool merge.diffstat)" = false
&& show_diffstat
=false
255 test "$(git config --bool merge.stat)" = false
&& show_diffstat
=false
256 test -z "$show_diffstat" && show_diffstat
=t
259 # This could be traditional "merge <msg> HEAD <commit>..." and the
260 # way we can tell it is to see if the second token is HEAD, but some
261 # people might have misused the interface and used a committish that
262 # is the same as HEAD there instead. Traditional format never would
263 # have "-m" so it is an additional safety measure to check for it.
265 if test -z "$have_message" &&
266 second_token
=$
(git rev-parse
--verify "$2^0" 2>/dev
/null
) &&
267 head_commit
=$
(git rev-parse
--verify "HEAD" 2>/dev
/null
) &&
268 test "$second_token" = "$head_commit"
274 elif ! git rev-parse
--verify HEAD
>/dev
/null
2>&1
276 # If the merged head is a valid one there is no reason to
277 # forbid "git merge" into a branch yet to be born. We do
278 # the same for "git pull".
281 echo >&2 "Can merge only exactly one commit into empty head"
285 test "$squash" != t ||
286 die
"Squash commit into empty head not supported yet"
287 test "$allow_fast_forward" = t ||
288 die
"Non-fast-forward into an empty head does not make sense"
289 rh
=$
(git rev-parse
--verify "$1^0") ||
290 die
"$1 - not something we can merge"
292 git update-ref
-m "initial pull" HEAD
"$rh" "" &&
293 git read-tree
--reset -u HEAD
297 # We are invoked directly as the first-class UI.
300 # All the rest are the commits being merged; prepare
301 # the standard merge summary message to be appended to
302 # the given message. If remote is invalid we will die
303 # later in the common codepath so we discard the error
310 if test "$have_message" = t
312 git fmt-merge-msg -m "$merge_msg" $log_arg
314 git fmt-merge-msg $log_arg
318 head=$
(git rev-parse
--verify "$head_arg"^
0) || usage
320 # All the rest are remote heads
321 test "$#" = 0 && usage
;# we need at least one remote head.
322 set_reflog_action
"merge $*"
327 remotehead
=$
(git rev-parse
--verify "$remote"^
0 2>/dev
/null
) ||
328 die
"$remote - not something we can merge"
329 remoteheads
="${remoteheads}$remotehead "
330 eval GITHEAD_
$remotehead='"$remote"'
331 export GITHEAD_
$remotehead
333 set x
$remoteheads ; shift
335 case "$use_strategies" in
339 var
="`git config --get pull.twohead`"
342 use_strategies
="$var"
344 use_strategies
="$default_twohead_strategies"
347 var
="`git config --get pull.octopus`"
350 use_strategies
="$var"
352 use_strategies
="$default_octopus_strategies"
358 for s
in $use_strategies
360 for ss
in $no_fast_forward_strategies
369 for ss
in $no_trivial_strategies
373 allow_trivial_merge
=f
382 common
=$
(git merge-base
--all $head "$@")
385 common
=$
(git merge-base
--all --octopus $head "$@")
388 echo "$head" >"$GIT_DIR/ORIG_HEAD"
390 case "$allow_fast_forward,$#,$common,$no_commit" in
392 # No common ancestors found. We need a real merge.
395 # If head can reach all the merge then we are up to date.
396 # but first the most common case of merging one remote.
397 finish_up_to_date
"Already up-to-date."
401 # Again the most common case of merging one remote.
402 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
403 git update-index
--refresh 2>/dev
/null
405 if test -n "$have_message"
407 msg
="$msg (no commit created; -m option ignored)"
409 new_head
=$
(git rev-parse
--verify "$1^0") &&
410 git read-tree
-v -m -u --exclude-per-directory=.gitignore
$head "$new_head" &&
411 finish
"$new_head" "$msg" ||
exit
416 # We are not doing octopus and not fast-forward. Need a
420 # We are not doing octopus, not fast-forward, and have only
422 git update-index
--refresh 2>/dev
/null
423 case "$allow_trivial_merge,$fast_forward_only" in
425 # See if it is really trivial.
426 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
427 echo "Trying really trivial in-index merge..."
428 if git read-tree
--trivial -m -u -v $common $head "$1" &&
429 result_tree
=$
(git write-tree
)
433 printf '%s\n' "$merge_msg" |
434 git commit-tree
$result_tree -p HEAD
-p "$1"
436 finish
"$result_commit" "In-index merge"
444 # An octopus. If we can reach all the remote we are up to date.
448 common_one
=$
(git merge-base
--all $head $remote)
449 if test "$common_one" != "$remote"
455 if test "$up_to_date" = t
457 finish_up_to_date
"Already up-to-date. Yeeah!"
463 if test "$fast_forward_only" = t
465 die
"Not possible to fast-forward, aborting."
468 # We are going to make a new commit.
469 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
471 # At this point, we need a real merge. No matter what strategy
472 # we use, it would operate on the index, possibly affecting the
473 # working tree, and when resolved cleanly, have the desired tree
474 # in the index -- this means that the index must be in sync with
475 # the $head commit. The strategies are responsible to ensure this.
477 case "$use_strategies" in
479 # Stash away the local changes so that we can try more than one.
484 rm -f "$GIT_DIR/MERGE_STASH"
489 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
491 for strategy
in $use_strategies
493 test "$wt_strategy" = '' ||
{
494 echo "Rewinding the tree to pristine..."
497 case "$single_strategy" in
499 echo "Trying merge strategy $strategy..."
503 # Remember which strategy left the state in the working tree
504 wt_strategy
=$strategy
506 eval 'git-merge-$strategy '"$xopt"' $common -- "$head_arg" "$@"'
508 if test "$no_commit" = t
&& test "$exit" = 0
511 exit=1 ;# pretend it left conflicts.
514 test "$exit" = 0 ||
{
516 # The backend exits with 1 when conflicts are left to be resolved,
517 # with 2 when it does not handle the given merge at all.
519 if test "$exit" -eq 1
522 git diff-files --name-only
523 git ls-files --unmerged
525 if test $best_cnt -le 0 -o $cnt -le $best_cnt
527 best_strategy
=$strategy
534 # Automerge succeeded.
535 result_tree
=$
(git write-tree
) && break
538 # If we have a resulting tree, that means the strategy module
539 # auto resolved the merge cleanly.
540 if test '' != "$result_tree"
542 if test "$allow_fast_forward" = "t"
544 parents
=$
(git merge-base
--independent "$head" "$@")
546 parents
=$
(git rev-parse
"$head" "$@")
548 parents
=$
(echo "$parents" |
sed -e 's/^/-p /')
549 result_commit
=$
(printf '%s\n' "$merge_msg" | git commit-tree
$result_tree $parents) ||
exit
550 finish
"$result_commit" "Merge made by $wt_strategy."
555 # Pick the result from the best strategy and have the user fix it up.
556 case "$best_strategy" in
559 case "$use_strategies" in
561 echo >&2 "No merge strategy handled the merge."
564 echo >&2 "Merge with strategy $use_strategies failed."
570 # We already have its result in the working tree.
573 echo "Rewinding the tree to pristine..."
575 echo "Using the $best_strategy to prepare resolving by hand."
576 git-merge-
$best_strategy $common -- "$head_arg" "$@"
580 if test "$squash" = t
587 done >"$GIT_DIR/MERGE_HEAD"
588 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
591 if test "$merge_was_ok" = t
594 "Automatic merge went well; stopped before committing as requested"
601 git ls-files
--unmerged |
602 sed -e 's/^[^ ]* / /' |
604 } >>"$GIT_DIR/MERGE_MSG"
606 die
"Automatic merge failed; fix conflicts and then commit the result."