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=
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)
51 verbosity="$verbosity -q" ;;
53 verbosity="$verbosity -v" ;;
55 progress=--progress ;;
56 -n|--no-stat|--no-summary)
62 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
63 no_commit=--no-commit ;;
64 --c|--co|--com|--comm|--commi|--commit)
66 --sq|--squ|--squa|--squas|--squash)
68 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
76 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
77 --strateg=*|--strategy=*|\
78 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
81 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
88 strategy_args="${strategy_args}-s $strategy "
95 xx="-X $(git rev-parse --sq-quote "$2")"
98 xx=$(git rev-parse --sq-quote "$1") ;;
100 merge_args="$merge_args$xx "
102 -r|--r|--re|--reb|--reba|--rebas|--rebase)
105 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
108 --d|--dr|--dry|--dry-|--dry-r|--dry-ru|--dry-run)
111 -h|--h|--he|--hel|--help)
115 # Pass thru anything that may be meant for fetch.
122 error_on_no_merge_candidates () {
127 -t|--t|--ta|--tag|--tags)
128 echo "Fetching tags only, you probably meant:"
129 echo " git fetch --tags"
134 if test true = "$rebase"
143 curr_branch=${curr_branch#refs/heads/}
144 upstream=$(git config "branch.$curr_branch.merge")
145 remote=$(git config "branch.$curr_branch.remote")
147 if [ $# -gt 1 ]; then
148 if [ "$rebase" = true ]; then
149 printf "There is no candidate for rebasing against "
151 printf "There are no candidates for merging "
153 echo "among the refs that you just fetched."
154 echo "Generally this means that you provided a wildcard refspec which had no"
155 echo "matches on the remote end."
156 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
157 echo "You asked to pull from the remote '$1', but did not specify"
158 echo "a branch. Because this is not the default configured remote"
159 echo "for your current branch, you must specify a branch on the command line."
160 elif [ -z "$curr_branch" ]; then
161 echo "You are not currently on a branch, so I cannot use any"
162 echo "'branch.<branchname>.merge' in your configuration file."
163 echo "Please specify which remote branch you want to use on the command"
164 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
165 echo "See git-pull(1) for details."
166 elif [ -z "$upstream" ]; then
167 echo "You asked me to pull without telling me which branch you"
168 echo "want to $op_type $op_prep, and 'branch.${curr_branch}.merge' in"
169 echo "your configuration file does not tell me, either. Please"
170 echo "specify which branch you want to use on the command line and"
171 echo "try again (e.g. 'git pull <repository> <refspec>')."
172 echo "See git-pull(1) for details."
174 echo "If you often $op_type $op_prep the same branch, you may want to"
175 echo "use something like the following in your configuration file:"
177 echo " [branch \"${curr_branch}\"]"
178 echo " remote = <nickname>"
179 echo " merge = <remote-ref>"
180 test rebase = "$op_type" &&
181 echo " rebase = true"
183 echo " [remote \"<nickname>\"]"
185 echo " fetch = <refspec>"
187 echo "See git-config(1) for details."
189 echo "Your configuration specifies to $op_type $op_prep the ref '${upstream#refs/heads/}'"
190 echo "from the remote, but no such ref was fetched."
195 test true = "$rebase" && {
196 if ! git rev-parse -q --verify HEAD >/dev/null
198 # On an unborn branch
199 if test -f "$GIT_DIR/index"
201 die "updating an unborn branch with changes added to the index"
204 require_clean_work_tree "pull with rebase" "Please commit or stash them."
207 . git-parse-remote &&
208 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
209 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
210 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
212 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
214 oldremoteref="$reflog"
219 orig_head=$(git rev-parse -q --verify HEAD)
220 git fetch $verbosity $progress $dry_run --update-head-ok "$@" || exit 1
221 test -z "$dry_run" || exit 0
223 curr_head=$(git rev-parse -q --verify HEAD)
224 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
226 # The fetch involved updating the current branch.
228 # The working tree and the index file is still based on the
229 # $orig_head commit, but we are merging into $curr_head.
230 # First update the working tree to match $curr_head.
232 echo >&2 "Warning: fetch updated the current branch head."
233 echo >&2 "Warning: fast-forwarding your working tree from"
234 echo >&2 "Warning: commit $orig_head."
235 git update-index -q --refresh
236 git read-tree -u -m "$orig_head" "$curr_head" ||
237 die 'Cannot fast-forward your working tree.
238 After making sure that you saved anything precious from
239 $ git diff '$orig_head'
246 merge_head=$(sed -e '/ not-for-merge /d' \
247 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
250 case "$merge_head" in
252 error_on_no_merge_candidates "$@"
255 if test -z "$orig_head"
257 die "Cannot merge multiple branches into empty head"
259 if test true = "$rebase"
261 die "Cannot rebase onto multiple branches"
266 if test -z "$orig_head"
268 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
269 git read-tree --reset -u HEAD || exit 1
273 if test true = "$rebase"
275 o=$(git show-branch --merge-base $curr_branch $merge_head $oldremoteref)
276 if test "$oldremoteref" = "$o"
282 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
285 eval="git-rebase $diffstat $strategy_args $merge_args"
286 eval="$eval --onto $merge_head ${oldremoteref:-$merge_head}"
289 eval="git-merge $diffstat $no_commit $squash $no_ff $ff_only"
290 eval="$eval $log_arg $strategy_args $merge_args"
291 eval="$eval \"\$merge_name\" HEAD $merge_head $verbosity"