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 $*"
16 test -z "$(git ls-files -u)" ||
17 die "You are in the middle of a conflicted merge."
19 strategy_args= diffstat= no_commit= squash= no_ff= log_arg= verbosity=
20 curr_branch=$(git symbolic-ref -q HEAD)
21 curr_branch_short=$(echo "$curr_branch" | sed "s|refs/heads/||")
22 rebase=$(git config --bool branch.$curr_branch_short.rebase)
27 verbosity="$verbosity -q" ;;
29 verbosity="$verbosity -v" ;;
30 -n|--no-stat|--no-summary)
36 --no-c|--no-co|--no-com|--no-comm|--no-commi|--no-commit)
37 no_commit=--no-commit ;;
38 --c|--co|--com|--comm|--commi|--commit)
40 --sq|--squ|--squa|--squas|--squash)
42 --no-sq|--no-squ|--no-squa|--no-squas|--no-squash)
48 -s=*|--s=*|--st=*|--str=*|--stra=*|--strat=*|--strate=*|\
49 --strateg=*|--strategy=*|\
50 -s|--s|--st|--str|--stra|--strat|--strate|--strateg|--strategy)
53 strategy=`expr "z$1" : 'z-[^=]*=\(.*\)'` ;;
60 strategy_args="${strategy_args}-s $strategy "
62 -r|--r|--re|--reb|--reba|--rebas|--rebase)
65 --no-r|--no-re|--no-reb|--no-reba|--no-rebas|--no-rebase)
68 -h|--h|--he|--hel|--help)
72 # Pass thru anything that may be meant for fetch.
79 error_on_no_merge_candidates () {
84 -t|--t|--ta|--tag|--tags)
85 echo "Fetching tags only, you probably meant:"
86 echo " git fetch --tags"
91 curr_branch=${curr_branch#refs/heads/}
92 upstream=$(git config "branch.$curr_branch.merge")
93 remote=$(git config "branch.$curr_branch.remote")
96 echo "There are no candidates for merging in the refs that you just fetched."
97 echo "Generally this means that you provided a wildcard refspec which had no"
98 echo "matches on the remote end."
99 elif [ $# -gt 0 ] && [ "$1" != "$remote" ]; then
100 echo "You asked to pull from the remote '$1', but did not specify"
101 echo "a branch to merge. Because this is not the default configured remote"
102 echo "for your current branch, you must specify a branch on the command line."
103 elif [ -z "$curr_branch" ]; then
104 echo "You are not currently on a branch, so I cannot use any"
105 echo "'branch.<branchname>.merge' in your configuration file."
106 echo "Please specify which branch you want to merge on the command"
107 echo "line and try again (e.g. 'git pull <repository> <refspec>')."
108 echo "See git-pull(1) for details."
109 elif [ -z "$upstream" ]; then
110 echo "You asked me to pull without telling me which branch you"
111 echo "want to merge with, and 'branch.${curr_branch}.merge' in"
112 echo "your configuration file does not tell me either. Please"
113 echo "specify which branch you want to merge on the command line and"
114 echo "try again (e.g. 'git pull <repository> <refspec>')."
115 echo "See git-pull(1) for details."
117 echo "If you often merge with the same branch, you may want to"
118 echo "configure the following variables in your configuration"
121 echo " branch.${curr_branch}.remote = <nickname>"
122 echo " branch.${curr_branch}.merge = <remote-ref>"
123 echo " remote.<nickname>.url = <url>"
124 echo " remote.<nickname>.fetch = <refspec>"
126 echo "See git-config(1) for details."
128 echo "Your configuration specifies to merge the ref '${upstream#refs/heads/}' from the"
129 echo "remote, but no such ref was fetched."
134 test true = "$rebase" && {
135 if ! git rev-parse -q --verify HEAD >/dev/null
137 # On an unborn branch
138 if test -f "$GIT_DIR/index"
140 die "updating an unborn branch with changes added to the index"
143 git update-index --ignore-submodules --refresh &&
144 git diff-files --ignore-submodules --quiet &&
145 git diff-index --ignore-submodules --cached --quiet HEAD -- ||
146 die "refusing to pull with rebase: your working tree is not up-to-date"
149 . git-parse-remote &&
150 remoteref="$(get_remote_merge_branch "$@" 2>/dev/null)" &&
151 oldremoteref="$(git rev-parse -q --verify "$remoteref")" &&
152 for reflog in $(git rev-list -g $remoteref 2>/dev/null)
154 if test "$reflog" = "$(git merge-base $reflog $curr_branch)"
156 oldremoteref="$reflog"
161 orig_head=$(git rev-parse -q --verify HEAD)
162 git fetch $verbosity --update-head-ok "$@" || exit 1
164 curr_head=$(git rev-parse -q --verify HEAD)
165 if test -n "$orig_head" && test "$curr_head" != "$orig_head"
167 # The fetch involved updating the current branch.
169 # The working tree and the index file is still based on the
170 # $orig_head commit, but we are merging into $curr_head.
171 # First update the working tree to match $curr_head.
173 echo >&2 "Warning: fetch updated the current branch head."
174 echo >&2 "Warning: fast-forwarding your working tree from"
175 echo >&2 "Warning: commit $orig_head."
176 git update-index -q --refresh
177 git read-tree -u -m "$orig_head" "$curr_head" ||
178 die 'Cannot fast-forward your working tree.
179 After making sure that you saved anything precious from
180 $ git diff '$orig_head'
187 merge_head=$(sed -e '/ not-for-merge /d' \
188 -e 's/ .*//' "$GIT_DIR"/FETCH_HEAD | \
191 case "$merge_head" in
193 error_on_no_merge_candidates "$@"
196 if test -z "$orig_head"
198 die "Cannot merge multiple branches into empty head"
200 if test true = "$rebase"
202 die "Cannot rebase onto multiple branches"
207 if test -z "$orig_head"
209 git update-ref -m "initial pull" HEAD $merge_head "$curr_head" &&
210 git read-tree --reset -u HEAD || exit 1
214 merge_name=$(git fmt-merge-msg $log_arg <"$GIT_DIR/FETCH_HEAD") || exit
215 test true = "$rebase" &&
216 exec git-rebase $diffstat $strategy_args --onto $merge_head \
217 ${oldremoteref:-$merge_head}
218 exec git-merge $diffstat $no_commit $squash $no_ff $log_arg $strategy_args \
219 "$merge_name" HEAD $merge_head $verbosity