3 USAGE='[-q] [-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
9 old=$(git-rev-parse --verify $old_name 2>/dev/null)
10 oldbranch=$(git-symbolic-ref $old_name 2>/dev/null)
22 while [ "$#" != "0" ]; do
29 [ -z "$newbranch" ] &&
30 die "git checkout: -b needs a branch name"
31 git-show-ref --verify --quiet -- "refs/heads/$newbranch" &&
32 die "git checkout: branch $newbranch already exists"
33 git-check-ref-format "heads/$newbranch" ||
34 die "git checkout: we do not like '$newbranch' as a branch name."
39 "--track"|"--no-track")
58 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
60 if [ -z "$rev" ]; then
61 echo "unknown flag $arg"
66 if git-show-ref --verify --quiet -- "refs/heads/$arg"
70 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
72 # checking out selected paths from a tree-ish.
74 new_name="$arg^{tree}"
92 case "$newbranch,$track" in
94 die "git checkout: --track and --no-track require -b"
97 case "$force$merge" in
99 die "git checkout: -f and -m are incompatible"
102 # The behaviour of the command with and without explicit path
103 # parameters is quite different.
105 # Without paths, we are checking out everything in the work tree,
106 # possibly switching branches. This is the traditional behaviour.
108 # With paths, we are _never_ switching branch, but checking out
109 # the named paths from either index (when no rev is given),
110 # or the named tree-ish (when rev is given).
118 Did you intend to checkout '$@' which can not be resolved as commit?"
120 if test '' != "$newbranch$force$merge"
122 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
126 # from a specific tree-ish; note that this is for
127 # rescuing paths and is never meant to remove what
128 # is not in the named tree-ish.
129 git-ls-tree --full-name -r "$new" "$@" |
130 git-update-index --index-info || exit $?
133 # Make sure the request is about existing paths.
134 git-ls-files --error-unmatch -- "$@" >/dev/null || exit
135 git-ls-files -- "$@" |
136 git-checkout-index -f -u --stdin
139 # Make sure we did not fall back on $arg^{tree} codepath
140 # since we are not checking out from an arbitrary tree-ish,
141 # but switching branches.
144 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
145 die "Cannot switch branch to a non-commit."
149 # We are switching branches and checking out trees, so
150 # we *NEED* to be at the toplevel.
153 [ -z "$new" ] && new=$old && new_name="$old_name"
155 # If we don't have an existing branch that we're switching to,
156 # and we don't have a new branch name for the target we
157 # are switching to, then we are detaching our HEAD from any
158 # branch. However, if "git checkout HEAD" detaches the HEAD
159 # from the current branch, even though that may be logically
160 # correct, it feels somewhat funny. More importantly, we do not
161 # want "git checkout" nor "git checkout -f" to detach HEAD.
166 describe_detached_head () {
167 test -n "$quiet" || {
169 GIT_PAGER= git log >&2 -1 --pretty=oneline --abbrev-commit "$2"
173 if test -z "$branch$newbranch" && test "$new_name" != "$old_name"
176 if test -n "$oldbranch" && test -z "$quiet"
178 detach_warn="Note: moving to \"$new_name\" which isn't a local branch
179 If you want to create a new branch from this checkout, you may do so
180 (now or later) by using -b with the checkout command again. Example:
181 git checkout -b <new_branch_name>"
183 elif test -z "$oldbranch" && test "$new" != "$old"
185 describe_detached_head 'Previous HEAD position was' "$old"
192 echo >&2 "warning: You appear to be on a branch yet to be born."
193 echo >&2 "warning: Forcing checkout of $new_name."
200 git-read-tree --reset -u $new
202 git-update-index --refresh >/dev/null
203 merge_error=$(git-read-tree -m -u --exclude-per-directory=.gitignore $old $new 2>&1) || (
206 echo >&2 "$merge_error"
210 # Match the index to the working tree, and do a three-way.
211 git diff-files --name-only | git update-index --remove --stdin &&
212 work=`git write-tree` &&
213 git read-tree --reset -u $new || exit
215 eval GITHEAD_$new='${new_name:-${branch:-$new}}' &&
216 eval GITHEAD_$work=local &&
217 export GITHEAD_$new GITHEAD_$work &&
218 git merge-recursive $old -- $new $work
220 # Do not register the cleanly merged paths in the index yet.
221 # this is not a real merge before committing, but just carrying
222 # the working tree changes along.
223 unmerged=`git ls-files -u`
224 git read-tree --reset $new
229 z40=0000000000000000000000000000000000000000
231 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
233 ) | git update-index --index-info
239 if test "$saved_err" = 0 && test -z "$quiet"
241 git diff-index --name-status "$new"
247 # Switch the HEAD pointer to the new branch if we
248 # checked out a branch head, and remove any potential
249 # old MERGE_HEAD's (subsequent commits will clearly not
250 # be based on them, since we re-set the index)
252 if [ "$?" -eq 0 ]; then
253 if [ "$newbranch" ]; then
254 git-branch $track $newbranch_log "$newbranch" "$new_name" || exit
259 GIT_DIR="$GIT_DIR" git-symbolic-ref -m "checkout: moving to $branch" HEAD "refs/heads/$branch"
263 elif test "refs/heads/$branch" = "$oldbranch"
265 echo >&2 "Already on branch \"$branch\""
267 echo >&2 "Switched to${newbranch:+ a new} branch \"$branch\""
269 elif test -n "$detached"
271 # NEEDSWORK: we would want a command to detach the HEAD
272 # atomically, instead of this handcrafted command sequence.
274 # git update-ref --detach HEAD $new
275 # or something like that...
277 git-rev-parse HEAD >"$GIT_DIR/HEAD.new" &&
278 mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" &&
279 git-update-ref -m "checkout: moving to $arg" HEAD "$detached" ||
280 die "Cannot detach HEAD"
281 if test -n "$detach_warn"
283 echo >&2 "$detach_warn"
285 describe_detached_head 'HEAD is now at' HEAD
287 rm -f "$GIT_DIR/MERGE_HEAD"