3 # Copyright (c) 2005 Junio C Hamano
5 # Fetch one or more remote refs and merge it/them into the current HEAD.
7 USAGE='[-n | --no-stat] [--[no-]commit] [--[no-]squash] [--[no-]ff] [--[no-]rebase|--rebase=preserve] [-s strategy]... [<fetch-options>] <repo> <head>...'
8 LONG_USAGE='Fetch one or more remote refs and integrate it/them with the current HEAD.'
13 set_reflog_action "pull${1+ $*}"
14 require_work_tree_exists
19 git diff-index --cached --name-status -r --ignore-submodules HEAD --
20 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
21 die "$(gettext "Pull is not possible because you have unmerged files.
22 Please, fix them up in the work tree, and then use 'git add/rm <file>'
23 as appropriate to mark resolution, or use 'git commit -a'.")"
25 die "$(gettext "Pull is not possible because you have unmerged files.")"
30 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
31 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
32 Please, commit your changes before you can merge.")"
34 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
38 test -z "$(git ls-files -u)" || die_conflict
39 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
41 bool_or_string_config () {
42 git config --bool "$1" 2>/dev/null || git config "$1"
45 strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
46 log_arg= verbosity= progress= recurse_submodules= verify_signatures=
47 merge_args= edit= rebase_args=
48 curr_branch=$(git symbolic-ref -q HEAD)
49 curr_branch_short="${curr_branch#refs/heads/}"
50 mode=$(git config branch.${curr_branch_short}.pullmode)
53 mode=$(git config pull.mode)
56 merge|rebase|merge-ff-only|'')
60 rebase_args="--preserve-merges"
63 echo "Invalid value for 'mode'"
71 rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
74 rebase=$(bool_or_string_config pull.rebase)
78 echo "The configurations pull.rebase and branch.<name>.rebase are deprecated."
79 echo "Please use pull.mode and branch.<name>.pullmode instead."
82 test -z "$mode" && mode=default
88 verbosity="$verbosity -q" ;;
90 verbosity="$verbosity -v" ;;
92 progress=--progress ;;
94 progress=--no-progress ;;
95 -n|--no-stat|--no-summary)
101 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
102 no_commit=--no-commit ;;
103 --c|--co|--com|--comm|--commi|--commit)
104 no_commit=--commit ;;
109 --sq|--squ|--squa|--squas|--squash)
111 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
112 squash=--no-squash ;;
119 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
120 --strateg=*|--strategy=*|\
121 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
124 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
131 strategy_args="${strategy_args}-s $strategy "
138 xx="-X $(git rev-parse --sq-quote "$2")"
141 xx=$(git rev-parse --sq-quote "$1") ;;
143 merge_args="$merge_args$xx "
145 -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
148 -r|--r|--re|--reb|--reba|--rebas|--rebase)
151 -m|--m|--me|--mer|--merg|--merge)
154 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
156 echo "--no-rebase is deprecated, please use --merge instead"
158 --recurse-submodules)
159 recurse_submodules=--recurse-submodules
161 --recurse-submodules=*)
162 recurse_submodules="$1"
164 --no-recurse-submodules)
165 recurse_submodules=--no-recurse-submodules
168 verify_signatures=--verify-signatures
170 --no-verify-signatures)
171 verify_signatures=--no-verify-signatures
173 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
180 # Pass thru anything that may be meant for fetch.
198 rebase_args=--preserve-merges
201 echo "Invalid value for --rebase, should be true, false, or preserve"
208 error_on_no_merge_candidates () {
213 -t|--t|--ta|--tag|--tags)
214 echo "It doesn't make sense to pull all tags; you probably meant:"
215 echo " git fetch --tags"
220 if test "$mode" = rebase
229 upstream=$(git config "branch.$curr_branch_short.merge")
230 remote=$(git config "branch.$curr_branch_short.remote")
232 if [ $# -gt 1 ]; then
233 if [ "$mode" = rebase ]; then
234 printf "There is no candidate for rebasing against "
236 printf "There are no candidates for merging "
238 echo "among the refs that you just fetched."
239 echo "Generally this means that you provided a wildcard refspec which had no"
240 echo "matches on the remote end."
241 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
242 echo "You asked to pull from the remote '$1', but did not specify"
243 echo "a branch. Because this is not the default configured remote"
244 echo "for your current branch, you must specify a branch on the command line."
245 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
247 error_on_missing_default_upstream "pull" $op_type $op_prep \
248 "git pull <remote> <branch>"
250 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
251 echo "from the remote, but no such ref was fetched."
256 test "$mode" = rebase && {
257 if ! git rev-parse -q --verify HEAD >/dev/null
259 # On an unborn branch
260 if test -f "$GIT_DIR/index"
262 die "$(gettext "updating an unborn branch with changes added to the index")"
265 require_clean_work_tree "pull with rebase" "Please commit or stash them."
268 test -n "$curr_branch" &&
269 . git-parse-remote &&
270 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
271 oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
273 orig_head=$(git rev-parse -q --verify HEAD)
274 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
275 test -z "$dry_run" || exit 0
277 curr_head=$(git rev-parse -q --verify HEAD)
278 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
280 # The fetch involved updating the current branch.
282 # The working tree and the index file is still based on the
283 # $orig_head commit, but we are merging into $curr_head.
284 # First update the working tree to match $curr_head.
286 eval_gettextln "Warning: fetch updated the current branch head.
287 Warning: fast-forwarding your working tree from
288 Warning: commit \$orig_head." >&2
289 git update-index -q --refresh
290 git read-tree -u -m "$orig_head" "$curr_head" ||
291 die "$(eval_gettext "Cannot fast-forward your working tree.
292 After making sure that you saved anything precious from
293 $ git diff \$orig_head
300 merge_head=$(sed -e '/ not-for-merge /d' \
301 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
304 case "$merge_head" in
306 error_on_no_merge_candidates "$@"
309 if test -z "$orig_head"
311 die "$(gettext "Cannot merge multiple branches into empty head")"
313 if test "$mode" = rebase
315 die "$(gettext "Cannot rebase onto multiple branches")"
319 # check if a non-fast-foward merge would be needed
320 merge_head=${merge_head% }
321 if test -z "$no_ff$ff_only${squash#--no-squash}" &&
322 test -n "$orig_head" &&
323 ! git merge-base --is-ancestor "$orig_head" "$merge_head" &&
324 ! git merge-base --is-ancestor "$merge_head" "$orig_head"
328 die "$(gettext "The pull was not fast-forward, please either merge or rebase.
329 If unsure, run 'git pull --merge'.")"
332 echo "$(gettext "warning: the pull was not fast-forward, in the future you would have to choose
333 a merge or a rebase, falling back to old style for now (git pull --merge).
334 Read 'git pull --help' for more information.")" >&2
341 # Pulling into unborn branch: a shorthand for branching off
342 # FETCH_HEAD, for lazy typers.
343 if test -z "$orig_head"
345 # Two-way merge: we claim the index is based on an empty tree,
346 # and try to fast-forward to HEAD. This ensures we will not
347 # lose index/worktree changes that the user already made on
349 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
350 git read-tree -m -u $empty_tree $merge_head &&
351 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
355 if test "$mode" = rebase
357 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
358 if test "$oldremoteref" = "$o"
364 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
367 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
368 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
371 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
372 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
373 eval="$eval \"\$merge_name\" HEAD $merge_head"