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,no-stat don't show a diffstat at the end of the merge
13 summary (synonym to --stat)
14 no-summary (synonym to --no-stat)
15 squash create a single commit instead of doing a merge
16 commit perform a commit if the merge sucesses (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
"You are in the middle of a conflicted merge."
33 all_strategies
='recur recursive octopus resolve stupid ours subtree'
34 default_twohead_strategies
='recursive'
35 default_octopus_strategies
='octopus'
36 no_fast_forward_strategies
='subtree ours'
37 no_trivial_strategies
='recursive recur subtree ours'
45 rm -f -- "$GIT_DIR/MERGE_HEAD" "$GIT_DIR/MERGE_MSG" \
46 "$GIT_DIR/MERGE_STASH" ||
exit 1
50 # Stash away any local modifications.
51 git stash create
>"$GIT_DIR/MERGE_STASH"
55 if test -f "$GIT_DIR/MERGE_STASH"
57 git
reset --hard $head >/dev
/null
58 git stash apply $
(cat "$GIT_DIR/MERGE_STASH")
59 git update-index
--refresh >/dev
/null
63 finish_up_to_date
() {
66 echo "$1 (nothing to squash)" ;;
74 echo Squashed commit of the following
:
76 git log
--no-merges --pretty=medium ^
"$head" $remoteheads
82 rlogm
="$GIT_REFLOG_ACTION"
85 rlogm
="$GIT_REFLOG_ACTION: $2"
89 echo "Squash commit -- not updating HEAD"
90 squash_message
>"$GIT_DIR/SQUASH_MSG"
95 echo "No merge message -- not updating HEAD"
98 git update-ref
-m "$rlogm" HEAD
"$1" "$head" ||
exit 1
108 if test "$show_diffstat" = t
110 # We want color (if set), but no pager
111 GIT_PAGER
='' git
diff --stat --summary -M "$head" "$1"
116 # Run a post-merge hook
117 if test -x "$GIT_DIR"/hooks
/post-merge
121 "$GIT_DIR"/hooks
/post-merge
1
124 "$GIT_DIR"/hooks
/post-merge
0
132 rh
=$
(git rev-parse
--verify "$remote^0" 2>/dev
/null
) ||
return
133 bh
=$
(git show-ref
-s --verify "refs/heads/$remote" 2>/dev
/null
)
134 if test "$rh" = "$bh"
136 echo "$rh branch '$remote' of ."
137 elif truname
=$
(expr "$remote" : '\(.*\)~[1-9][0-9]*$') &&
138 git show-ref
-q --verify "refs/heads/$truname" 2>/dev
/null
140 echo "$rh branch '$truname' (early part) of ."
141 elif test "$remote" = "FETCH_HEAD" -a -r "$GIT_DIR/FETCH_HEAD"
143 sed -e 's/ not-for-merge / /' -e 1q \
144 "$GIT_DIR/FETCH_HEAD"
146 echo "$rh commit '$remote'"
151 while test $# != 0; do
153 -n|
--no-stat|
--no-summary)
154 show_diffstat
=false
;;
158 test "$allow_fast_forward" = t ||
159 die
"You cannot combine --squash with --no-ff."
160 squash
=t no_commit
=t
;;
162 squash
= no_commit
= ;;
168 allow_fast_forward
=t
;;
170 test "$squash" != t ||
171 die
"You cannot combine --squash with --no-ff."
172 allow_fast_forward
=f
;;
175 case " $all_strategies " in
177 use_strategies
="$use_strategies$1 " ;;
179 die
"available strategies are: $all_strategies" ;;
197 test $# != 0 || usage
201 if branch
=$
(git-symbolic-ref
-q HEAD
)
203 mergeopts
=$
(git config
"branch.${branch#refs/heads/}.mergeoptions")
204 if test -n "$mergeopts"
206 parse_config
$mergeopts --
211 while test $args_left -lt $#; do shift; done
213 if test -z "$show_diffstat"; then
214 test "$(git config --bool merge.diffstat)" = false
&& show_diffstat
=false
215 test -z "$show_diffstat" && show_diffstat
=t
218 # This could be traditional "merge <msg> HEAD <commit>..." and the
219 # way we can tell it is to see if the second token is HEAD, but some
220 # people might have misused the interface and used a committish that
221 # is the same as HEAD there instead. Traditional format never would
222 # have "-m" so it is an additional safety measure to check for it.
224 if test -z "$have_message" &&
225 second_token
=$
(git rev-parse
--verify "$2^0" 2>/dev
/null
) &&
226 head_commit
=$
(git rev-parse
--verify "HEAD" 2>/dev
/null
) &&
227 test "$second_token" = "$head_commit"
233 elif ! git rev-parse
--verify HEAD
>/dev
/null
2>&1
235 # If the merged head is a valid one there is no reason to
236 # forbid "git merge" into a branch yet to be born. We do
237 # the same for "git pull".
240 echo >&2 "Can merge only exactly one commit into empty head"
244 rh
=$
(git rev-parse
--verify "$1^0") ||
245 die
"$1 - not something we can merge"
247 git update-ref
-m "initial pull" HEAD
"$rh" "" &&
248 git read-tree
--reset -u HEAD
252 # We are invoked directly as the first-class UI.
255 # All the rest are the commits being merged; prepare
256 # the standard merge summary message to be appended to
257 # the given message. If remote is invalid we will die
258 # later in the common codepath so we discard the error
260 merge_name
=$
(for remote
263 done | git fmt-merge-msg
265 merge_msg
="${merge_msg:+$merge_msg$LF$LF}$merge_name"
267 head=$
(git rev-parse
--verify "$head_arg"^
0) || usage
269 # All the rest are remote heads
270 test "$#" = 0 && usage
;# we need at least one remote head.
271 set_reflog_action
"merge $*"
276 remotehead
=$
(git rev-parse
--verify "$remote"^
0 2>/dev
/null
) ||
277 die
"$remote - not something we can merge"
278 remoteheads
="${remoteheads}$remotehead "
279 eval GITHEAD_
$remotehead='"$remote"'
280 export GITHEAD_
$remotehead
282 set x
$remoteheads ; shift
284 case "$use_strategies" in
288 var
="`git config --get pull.twohead`"
291 use_strategies
="$var"
293 use_strategies
="$default_twohead_strategies"
296 var
="`git config --get pull.octopus`"
299 use_strategies
="$var"
301 use_strategies
="$default_octopus_strategies"
307 for s
in $use_strategies
309 for ss
in $no_fast_forward_strategies
318 for ss
in $no_trivial_strategies
322 allow_trivial_merge
=f
331 common
=$
(git merge-base
--all $head "$@")
334 common
=$
(git show-branch
--merge-base $head "$@")
337 echo "$head" >"$GIT_DIR/ORIG_HEAD"
339 case "$allow_fast_forward,$#,$common,$no_commit" in
341 # No common ancestors found. We need a real merge.
344 # If head can reach all the merge then we are up to date.
345 # but first the most common case of merging one remote.
346 finish_up_to_date
"Already up-to-date."
350 # Again the most common case of merging one remote.
351 echo "Updating $(git rev-parse --short $head)..$(git rev-parse --short $1)"
352 git update-index
--refresh 2>/dev
/null
354 if test -n "$have_message"
356 msg
="$msg (no commit created; -m option ignored)"
358 new_head
=$
(git rev-parse
--verify "$1^0") &&
359 git read-tree
-v -m -u --exclude-per-directory=.gitignore
$head "$new_head" &&
360 finish
"$new_head" "$msg" ||
exit
365 # We are not doing octopus and not fast forward. Need a
369 # We are not doing octopus, not fast forward, and have only
371 git update-index
--refresh 2>/dev
/null
372 case "$allow_trivial_merge" in
374 # See if it is really trivial.
375 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
376 echo "Trying really trivial in-index merge..."
377 if git read-tree
--trivial -m -u -v $common $head "$1" &&
378 result_tree
=$
(git write-tree
)
382 printf '%s\n' "$merge_msg" |
383 git commit-tree
$result_tree -p HEAD
-p "$1"
385 finish
"$result_commit" "In-index merge"
393 # An octopus. If we can reach all the remote we are up to date.
397 common_one
=$
(git merge-base
--all $head $remote)
398 if test "$common_one" != "$remote"
404 if test "$up_to_date" = t
406 finish_up_to_date
"Already up-to-date. Yeeah!"
412 # We are going to make a new commit.
413 git var GIT_COMMITTER_IDENT
>/dev
/null ||
exit
415 # At this point, we need a real merge. No matter what strategy
416 # we use, it would operate on the index, possibly affecting the
417 # working tree, and when resolved cleanly, have the desired tree
418 # in the index -- this means that the index must be in sync with
419 # the $head commit. The strategies are responsible to ensure this.
421 case "$use_strategies" in
423 # Stash away the local changes so that we can try more than one.
428 rm -f "$GIT_DIR/MERGE_STASH"
433 result_tree
= best_cnt
=-1 best_strategy
= wt_strategy
=
435 for strategy
in $use_strategies
437 test "$wt_strategy" = '' ||
{
438 echo "Rewinding the tree to pristine..."
441 case "$single_strategy" in
443 echo "Trying merge strategy $strategy..."
447 # Remember which strategy left the state in the working tree
448 wt_strategy
=$strategy
450 git-merge-
$strategy $common -- "$head_arg" "$@"
452 if test "$no_commit" = t
&& test "$exit" = 0
455 exit=1 ;# pretend it left conflicts.
458 test "$exit" = 0 ||
{
460 # The backend exits with 1 when conflicts are left to be resolved,
461 # with 2 when it does not handle the given merge at all.
463 if test "$exit" -eq 1
466 git diff-files --name-only
467 git ls-files --unmerged
469 if test $best_cnt -le 0 -o $cnt -le $best_cnt
471 best_strategy
=$strategy
478 # Automerge succeeded.
479 result_tree
=$
(git write-tree
) && break
482 # If we have a resulting tree, that means the strategy module
483 # auto resolved the merge cleanly.
484 if test '' != "$result_tree"
486 if test "$allow_fast_forward" = "t"
488 parents
=$
(git show-branch
--independent "$head" "$@")
490 parents
=$
(git rev-parse
"$head" "$@")
492 parents
=$
(echo "$parents" |
sed -e 's/^/-p /')
493 result_commit
=$
(printf '%s\n' "$merge_msg" | git commit-tree
$result_tree $parents) ||
exit
494 finish
"$result_commit" "Merge made by $wt_strategy."
499 # Pick the result from the best strategy and have the user fix it up.
500 case "$best_strategy" in
503 case "$use_strategies" in
505 echo >&2 "No merge strategy handled the merge."
508 echo >&2 "Merge with strategy $use_strategies failed."
514 # We already have its result in the working tree.
517 echo "Rewinding the tree to pristine..."
519 echo "Using the $best_strategy to prepare resolving by hand."
520 git-merge-
$best_strategy $common -- "$head_arg" "$@"
524 if test "$squash" = t
531 done >"$GIT_DIR/MERGE_HEAD"
532 printf '%s\n' "$merge_msg" >"$GIT_DIR/MERGE_MSG"
535 if test "$merge_was_ok" = t
538 "Automatic merge went well; stopped before committing as requested"
545 git ls-files
--unmerged |
546 sed -e 's/^[^ ]* / /' |
548 } >>"$GIT_DIR/MERGE_MSG"
550 die
"Automatic merge failed; fix conflicts and then commit the result."