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] [-s strategy]... [<fetch-options>] <repo> <head>...'
8 LONG_USAGE='Fetch one or more remote refs and merge it/them into the current HEAD.'
12 set_reflog_action "pull $*"
18 git diff-index --cached --name-status -r --ignore-submodules HEAD --
19 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
20 die "Pull is not possible because you have unmerged files.
21 Please, fix them up in the work tree, and then use 'git add/rm <file>'
22 as appropriate to mark resolution, or use 'git commit -a'."
24 die "Pull is not possible because you have unmerged files."
29 if [ $(git config --bool --get advice.resolveConflict || echo true) = "true" ]; then
30 die "You have not concluded your merge (MERGE_HEAD exists).
31 Please, commit your changes before you can merge."
33 die "You have not concluded your merge (MERGE_HEAD exists)."
37 test -z "$(git ls-files -u)" || die_conflict
38 test -f "$GIT_DIR/MERGE_HEAD" && die_merge
40 strategy_args= diffstat= no_commit= squash= no_ff= ff_only=
41 log_arg= verbosity= progress= recurse_submodules=
43 curr_branch=$(git symbolic-ref -q HEAD)
44 curr_branch_short="${curr_branch#refs/heads/}"
45 rebase=$(git config --bool branch.$curr_branch_short.rebase)
48 rebase=$(git config --bool pull.rebase)
55 verbosity="$verbosity -q" ;;
57 verbosity="$verbosity -v" ;;
59 progress=--progress ;;
61 progress=--no-progress ;;
62 -n|--no-stat|--no-summary)
68 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
69 no_commit=--no-commit ;;
70 --c|--co|--com|--comm|--commi|--commit)
72 --sq|--squ|--squa|--squas|--squash)
74 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
82 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
83 --strateg=*|--strategy=*|\
84 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
87 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
94 strategy_args="${strategy_args}-s $strategy "
101 xx="-X $(git rev-parse --sq-quote "$2")"
104 xx=$(git rev-parse --sq-quote "$1") ;;
106 merge_args="$merge_args$xx "
108 -r|--r|--re|--reb|--reba|--rebas|--rebase)
111 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
114 --recurse-submodules)
115 recurse_submodules=--recurse-submodules
117 --recurse-submodules=*)
118 recurse_submodules="$1"
120 --no-recurse-submodules)
121 recurse_submodules=--no-recurse-submodules
123 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
126 -h|--h|--he|--hel|--help|--help-|--help-a|--help-al|--help-all)
130 # Pass thru anything that may be meant for fetch.
137 error_on_no_merge_candidates () {
142 -t|--t|--ta|--tag|--tags)
143 echo "Fetching tags only, you probably meant:"
144 echo " git fetch --tags"
149 if test true = "$rebase"
158 curr_branch=${curr_branch#refs/heads/}
159 upstream=$(git config "branch.$curr_branch.merge")
160 remote=$(git config "branch.$curr_branch.remote")
162 if [ $# -gt 1 ]; then
163 if [ "$rebase" = true ]; then
164 printf "There is no candidate for rebasing against "
166 printf "There are no candidates for merging "
168 echo "among the refs that you just fetched."
169 echo "Generally this means that you provided a wildcard refspec which had no"
170 echo "matches on the remote end."
171 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
172 echo "You asked to pull from the remote '$1', but did not specify"
173 echo "a branch. Because this is not the default configured remote"
174 echo "for your current branch, you must specify a branch on the command line."
175 elif [ -z "$curr_branch" -o -z "$upstream" ]; then
177 error_on_missing_default_upstream "pull" $op_type $op_prep \
178 "git pull <repository> <refspec>"
180 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
181 echo "from the remote, but no such ref was fetched."
186 test true = "$rebase" && {
187 if ! git rev-parse -q --verify HEAD >/dev/null
189 # On an unborn branch
190 if test -f "$GIT_DIR/index"
192 die "updating an unborn branch with changes added to the index"
195 require_clean_work_tree "pull with rebase" "Please commit or stash them."
198 . git-parse-remote &&
199 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
200 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
201 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
203 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
205 oldremoteref="$reflog"
210 orig_head=$(git rev-parse -q --verify HEAD)
211 git fetch $verbosity $progress $dry_run $recurse_submodules --update-head-ok "$@" || exit 1
212 test -z "$dry_run" || exit 0
214 curr_head=$(git rev-parse -q --verify HEAD)
215 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
217 # The fetch involved updating the current branch.
219 # The working tree and the index file is still based on the
220 # $orig_head commit, but we are merging into $curr_head.
221 # First update the working tree to match $curr_head.
223 echo >&2 "Warning: fetch updated the current branch head."
224 echo >&2 "Warning: fast-forwarding your working tree from"
225 echo >&2 "Warning: commit $orig_head."
226 git update-index -q --refresh
227 git read-tree -u -m "$orig_head" "$curr_head" ||
228 die 'Cannot fast-forward your working tree.
229 After making sure that you saved anything precious from
230 $ git diff '$orig_head'
237 merge_head=$(sed -e '/ not-for-merge /d' \
238 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
241 case "$merge_head" in
243 error_on_no_merge_candidates "$@"
246 if test -z "$orig_head"
248 die "Cannot merge multiple branches into empty head"
250 if test true = "$rebase"
252 die "Cannot rebase onto multiple branches"
257 if test -z "$orig_head"
259 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
260 git read-tree -m -u HEAD || exit 1
264 if test true = "$rebase"
266 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
267 if test "$oldremoteref" = "$o"
273 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
276 eval="git-rebase $diffstat $strategy_args $merge_args"
277 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
280 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
281 eval="$eval $log_arg $strategy_args $merge_args $verbosity $progress"
282 eval="$eval \"\$merge_name\" HEAD $merge_head"