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|--ff-only] [--[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 and make a commit.")"
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 rebase=$(bool_or_string_config branch.$curr_branch_short.rebase)
53 rebase=$(bool_or_string_config pull.rebase)
56 # Setup default fast-forward options via `pull.ff`
57 pull_ff=$(git config pull.ff)
73 verbosity="$verbosity -q" ;;
75 verbosity="$verbosity -v" ;;
77 progress=--progress ;;
79 progress=--no-progress ;;
80 -n|--no-stat|--no-summary)
86 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
87 no_commit=--no-commit ;;
88 --c|--co|--com|--comm|--commi|--commit)
94 --sq|--squ|--squa|--squas|--squash)
96 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
104 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
105 --strateg=*|--strategy=*|\
106 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
109 strategy=$(expr "z$1" : 'z-[^=]*=\(.*\)') ;;
116 strategy_args="${strategy_args}-s $strategy "
123 xx="-X $(git rev-parse --sq-quote "$2")"
126 xx=$(git rev-parse --sq-quote "$1") ;;
128 merge_args="$merge_args$xx "
130 -r=*|--r=*|--re=*|--reb=*|--reba=*|--rebas=*|--rebase=*)
133 -r|--r|--re|--reb|--reba|--rebas|--rebase)
136 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
139 --recurse-submodules)
140 recurse_submodules=--recurse-submodules
142 --recurse-submodules=*)
143 recurse_submodules="$1"
145 --no-recurse-submodules)
146 recurse_submodules=--no-recurse-submodules
149 verify_signatures=--verify-signatures
151 --no-verify-signatures)
152 verify_signatures=--no-verify-signatures
158 gpg_sign_args=$(git rev-parse --sq-quote "-S${1#--gpg-sign=}")
161 gpg_sign_args=$(git rev-parse --sq-quote "$1")
163 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
170 # Pass thru anything that may be meant for fetch.
180 rebase_args=--preserve-merges
185 echo "Invalid value for --rebase, should be true, false, or preserve"
191 error_on_no_merge_candidates () {
194 if test true = "$rebase"
203 upstream=$(git config "branch.$curr_branch_short.merge")
204 remote=$(git config "branch.$curr_branch_short.remote")
206 if [ $# -gt 1 ]; then
207 if [ "$rebase" = true ]; then
208 printf "There is no candidate for rebasing against "
210 printf "There are no candidates for merging "
212 echo "among the refs that you just fetched."
213 echo "Generally this means that you provided a wildcard refspec which had no"
214 echo "matches on the remote end."
215 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
216 echo "You asked to pull from the remote '$1', but did not specify"
217 echo "a branch. Because this is not the default configured remote"
218 echo "for your current branch, you must specify a branch on the command line."
219 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
221 error_on_missing_default_upstream "pull" $op_type $op_prep \
222 "git pull <remote> <branch>"
224 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
225 echo "from the remote, but no such ref was fetched."
230 test true = "$rebase" && {
231 if ! git rev-parse -q --verify HEAD >/dev/null
233 # On an unborn branch
234 if test -f "$(git rev-parse --git-path index)"
236 die "$(gettext "updating an unborn branch with changes added to the index")"
239 require_clean_work_tree "pull with rebase" "Please commit or stash them."
242 test -n "$curr_branch" &&
243 . git-parse-remote &&
244 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
245 oldremoteref=$(git merge-base --fork-point "$remoteref" $curr_branch 2>/dev/null)
247 orig_head=$(git rev-parse -q --verify HEAD)
248 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
249 test -z "$dry_run" || exit 0
251 curr_head=$(git rev-parse -q --verify HEAD)
252 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
254 # The fetch involved updating the current branch.
256 # The working tree and the index file is still based on the
257 # $orig_head commit, but we are merging into $curr_head.
258 # First update the working tree to match $curr_head.
260 eval_gettextln "Warning: fetch updated the current branch head.
261 Warning: fast-forwarding your working tree from
262 Warning: commit \$orig_head." >&2
263 git update-index -q --refresh
264 git read-tree -u -m "$orig_head" "$curr_head" ||
265 die "$(eval_gettext "Cannot fast-forward your working tree.
266 After making sure that you saved anything precious from
267 $ git diff \$orig_head
274 merge_head=$(sed -e '/ not-for-merge /d' \
275 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
278 case "$merge_head" in
280 error_on_no_merge_candidates "$@"
283 if test -z "$orig_head"
285 die "$(gettext "Cannot merge multiple branches into empty head")"
287 if test true = "$rebase"
289 die "$(gettext "Cannot rebase onto multiple branches")"
294 # Pulling into unborn branch: a shorthand for branching off
295 # FETCH_HEAD, for lazy typers.
296 if test -z "$orig_head"
298 # Two-way merge: we claim the index is based on an empty tree,
299 # and try to fast-forward to HEAD. This ensures we will not
300 # lose index/worktree changes that the user already made on
302 empty_tree=4b825dc642cb6eb9a060e54bf8d69288fbee4904
303 git read-tree -m -u $empty_tree $merge_head &&
304 git update-ref -m "initial pull" HEAD $merge_head "$curr_head"
308 if test true = "$rebase"
310 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
311 if test "$oldremoteref" = "$o"
319 eval="git-rebase $diffstat $strategy_args $merge_args $rebase_args $verbosity"
320 eval="$eval $gpg_sign_args"
321 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
324 eval="git-merge $diffstat $no_commit $verify_signatures $edit $squash $no_ff $ff_only"
325 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
326 eval="$eval $gpg_sign_args"
327 eval="$eval FETCH_HEAD"