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 printf >&2 'warning: %s\n' "$*"
23 git diff-index --cached --name-status -r --ignore-submodules HEAD --
24 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
25 die "$(gettext "Pull is not possible because you have unmerged files.
26 Please, fix them up in the work tree, and then use 'git add/rm <file>'
27 as appropriate to mark resolution, or use 'git commit -a'.")"
29 die "$(gettext "Pull is not possible because you have unmerged files.")"
34 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
35 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).
36 Please, commit your changes before you can merge.")"
38 die "$(gettext "You have not concluded your merge (MERGE_HEAD exists).")"
42 test -z "$(git ls-files -u)" || die_conflict
43 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
45 bool_or_string_config () {
46 git config --bool "$1" 2>/dev/null || git config "$1"
49 strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
50 log_arg= verbosity= progress= recurse_submodules= verify_signatures=
51 merge_args= edit= rebase_args=
52 curr_branch=$(git symbolic-ref -q HEAD)
53 curr_branch_short="${curr_branch#refs/heads/}"
54 mode=$(git config branch.${curr_branch_short}.pullmode)
57 mode=$(git config pull.mode)
60 merge|rebase|ff-only|'')
64 rebase_args="--preserve-merges"
67 echo "Invalid value for 'mode'"
72 # backwards compatibility
75 rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
78 rebase=$(bool_or_string_config pull.rebase)
81 test -z "$mode" && mode=ff-only
87 verbosity="$verbosity -q" ;;
89 verbosity="$verbosity -v" ;;
91 progress=--progress ;;
93 progress=--no-progress ;;
94 -n|--no-stat|--no-summary)
100 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
101 no_commit=--no-commit ;;
102 --c|--co|--com|--comm|--commi|--commit)
103 no_commit=--commit ;;
108 --sq|--squ|--squa|--squas|--squash)
110 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
111 squash=--no-squash ;;
118 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
119 --strateg=*|--strategy=*|\
120 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
123 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
130 strategy_args="${strategy_args}-s $strategy "
137 xx="-X $(git rev-parse --sq-quote "$2")"
140 xx=$(git rev-parse --sq-quote "$1") ;;
142 merge_args="$merge_args$xx "
144 -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
147 -r|--r|--re|--reb|--reba|--rebas|--rebase)
150 -m|--m|--me|--mer|--merg|--merge)
153 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
155 warn "$(gettext "--no-rebase is deprecated, please use --merge instead")"
157 --recurse-submodules)
158 recurse_submodules=--recurse-submodules
160 --recurse-submodules=*)
161 recurse_submodules="$1"
163 --no-recurse-submodules)
164 recurse_submodules=--no-recurse-submodules
167 verify_signatures=--verify-signatures
169 --no-verify-signatures)
170 verify_signatures=--no-verify-signatures
172 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
179 # Pass thru anything that may be meant for fetch.
197 rebase_args=--preserve-merges
200 echo "Invalid value for --rebase, should be true, false, or preserve"
207 error_on_no_merge_candidates () {
212 -t|--t|--ta|--tag|--tags)
213 echo "It doesn't make sense to pull all tags; you probably meant:"
214 echo " git fetch --tags"
219 if test "$mode" = rebase
228 upstream=$(git config "branch.$curr_branch_short.merge")
229 remote=$(git config "branch.$curr_branch_short.remote")
231 if [ $# -gt 1 ]; then
232 if [ "$mode" = rebase ]; then
233 printf "There is no candidate for rebasing against "
235 printf "There are no candidates for merging "
237 echo "among the refs that you just fetched."
238 echo "Generally this means that you provided a wildcard refspec which had no"
239 echo "matches on the remote end."
240 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
241 echo "You asked to pull from the remote '$1', but did not specify"
242 echo "a branch. Because this is not the default configured remote"
243 echo "for your current branch, you must specify a branch on the command line."
244 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
246 error_on_missing_default_upstream "pull" $op_type $op_prep \
247 "git pull <remote> <branch>"
249 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
250 echo "from the remote, but no such ref was fetched."
255 test "$mode" = rebase && {
256 if ! git rev-parse -q --verify HEAD >/dev/null
258 # On an unborn branch
259 if test -f "$GIT_DIR/index"
261 die "$(gettext "updating an unborn branch with changes added to the index")"
264 require_clean_work_tree "pull with rebase" "Please commit or stash them."
267 test -n "$curr_branch" &&
268 . git-parse-remote &&
269 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
270 oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
272 orig_head=$(git rev-parse -q --verify HEAD)
273 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
274 test -z "$dry_run" || exit 0
276 curr_head=$(git rev-parse -q --verify HEAD)
277 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
279 # The fetch involved updating the current branch.
281 # The working tree and the index file is still based on the
282 # $orig_head commit, but we are merging into $curr_head.
283 # First update the working tree to match $curr_head.
285 eval_gettextln "Warning: fetch updated the current branch head.
286 Warning: fast-forwarding your working tree from
287 Warning: commit \$orig_head." >&2
288 git update-index -q --refresh
289 git read-tree -u -m "$orig_head" "$curr_head" ||
290 die "$(eval_gettext "Cannot fast-forward your working tree.
291 After making sure that you saved anything precious from
292 $ git diff \$orig_head
299 merge_head=$(sed -e '/ not-for-merge /d' \
300 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
303 case "$merge_head" in
305 error_on_no_merge_candidates "$@"
308 if test -z "$orig_head"
310 die "$(gettext "Cannot merge multiple branches into empty head")"
312 if test "$mode" = rebase
314 die "$(gettext "Cannot rebase onto multiple branches")"
318 # check if a non-fast-foward merge would be needed
319 merge_head=${merge_head% }
320 if test "$mode" = 'ff-only' && test -z "$no_ff$ff_only${squash#--no-squash}" &&
321 test -n "$orig_head" &&
322 ! git merge-base --is-ancestor "$orig_head" "$merge_head" &&
323 ! git merge-base --is-ancestor "$merge_head" "$orig_head"
325 die "$(gettext "The pull was not fast-forward, please either merge or rebase.
326 If unsure, run 'git pull --merge'.")"
331 # Pulling into unborn branch: a shorthand for branching off
332 # FETCH_HEAD, for lazy typers.
333 if test -z "$orig_head"
335 # Two-way merge: we claim the index is based on an empty tree,
336 # and try to fast-forward to HEAD. This ensures we will not
337 # lose index/worktree changes that the user already made on
339 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
340 git read-tree -m -u $empty_tree $merge_head &&
341 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
345 if test "$mode" = rebase
347 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
348 if test "$oldremoteref" = "$o"
354 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
357 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
358 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
361 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
362 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
363 eval="$eval \"\$merge_name\" HEAD $merge_head"