5 git-checkout [options] [<branch>] [<paths>...]
7 b= create a new branch started at <branch>
8 l create the new branch's reflog
9 track arrange that the new branch tracks the remote branch
10 f proceed even if the index or working tree is not HEAD
11 m merge local modifications into the new branch
14 SUBDIRECTORY_OK
=Sometimes
19 old
=$
(git rev-parse
--verify $old_name 2>/dev
/null
)
20 oldbranch
=$
(git symbolic-ref
$old_name 2>/dev
/null
)
34 while test $# != 0; do
39 [ -z "$newbranch" ] &&
40 die
"git checkout: -b needs a branch name"
41 git show-ref
--verify --quiet -- "refs/heads/$newbranch" &&
42 die
"git checkout: branch $newbranch already exists"
43 git check-ref-format
"heads/$newbranch" ||
44 die
"git checkout: we do not like '$newbranch' as a branch name."
74 if rev=$
(git rev-parse
--verify "$arg^0" 2>/dev
/null
)
76 [ -z "$rev" ] && die
"unknown flag $arg"
78 if git show-ref
--verify --quiet -- "refs/heads/$arg"
80 rev=$
(git rev-parse
--verify "refs/heads/$arg^0")
85 elif rev=$
(git rev-parse
--verify "$arg^{tree}" 2>/dev
/null
)
87 # checking out selected paths from a tree-ish.
89 new_name
="$arg^{tree}"
92 [ "$1" = "--" ] && shift
94 case "$newbranch,$track" in
96 die
"git checkout: --track and --no-track require -b"
99 case "$force$merge" in
101 die
"git checkout: -f and -m are incompatible"
104 # The behaviour of the command with and without explicit path
105 # parameters is quite different.
107 # Without paths, we are checking out everything in the work tree,
108 # possibly switching branches. This is the traditional behaviour.
110 # With paths, we are _never_ switching branch, but checking out
111 # the named paths from either index (when no rev is given),
112 # or the named tree-ish (when rev is given).
120 Did you intend to checkout '$@' which can not be resolved as commit?"
122 if test '' != "$newbranch$force$merge"
124 die
"git checkout: updating paths is incompatible with switching branches/forcing$hint"
128 # from a specific tree-ish; note that this is for
129 # rescuing paths and is never meant to remove what
130 # is not in the named tree-ish.
131 git ls-tree
--full-name -r "$new" "$@" |
132 git update-index
--index-info ||
exit $?
135 # Make sure the request is about existing paths.
136 git ls-files
--full-name --error-unmatch -- "$@" >/dev
/null ||
exit
137 git ls-files
--full-name -- "$@" |
138 (cd_to_toplevel
&& git checkout-index
-f -u --stdin)
140 # Run a post-checkout hook -- the HEAD does not change so the
141 # current HEAD is passed in for both args
142 if test -x "$GIT_DIR"/hooks
/post-checkout
; then
143 "$GIT_DIR"/hooks
/post-checkout
$old $old 0
148 # Make sure we did not fall back on $arg^{tree} codepath
149 # since we are not checking out from an arbitrary tree-ish,
150 # but switching branches.
153 git rev-parse
--verify "$new^{commit}" >/dev
/null
2>&1 ||
154 die
"Cannot switch branch to a non-commit."
158 # We are switching branches and checking out trees, so
159 # we *NEED* to be at the toplevel.
162 [ -z "$new" ] && new
=$old && new_name
="$old_name"
164 # If we don't have an existing branch that we're switching to,
165 # and we don't have a new branch name for the target we
166 # are switching to, then we are detaching our HEAD from any
167 # branch. However, if "git checkout HEAD" detaches the HEAD
168 # from the current branch, even though that may be logically
169 # correct, it feels somewhat funny. More importantly, we do not
170 # want "git checkout" nor "git checkout -f" to detach HEAD.
175 describe_detached_head
() {
176 test -n "$quiet" ||
{
178 GIT_PAGER
= git log
>&2 -1 --pretty=oneline
--abbrev-commit "$2" --
182 if test -z "$branch$newbranch" && test "$new_name" != "$old_name"
185 if test -n "$oldbranch" && test -z "$quiet"
187 detach_warn
="Note: moving to \"$new_name\" which isn't a local branch
188 If you want to create a new branch from this checkout, you may do so
189 (now or later) by using -b with the checkout command again. Example:
190 git checkout -b <new_branch_name>"
192 elif test -z "$oldbranch" && test "$new" != "$old"
194 describe_detached_head
'Previous HEAD position was' "$old"
201 echo >&2 "warning: You appear to be on a branch yet to be born."
202 echo >&2 "warning: Forcing checkout of $new_name."
209 git read-tree
$v --reset -u $new
211 git update-index
--refresh >/dev
/null
212 merge_error
=$
(git read-tree
-m -u --exclude-per-directory=.gitignore
$old $new 2>&1) ||
(
215 echo >&2 "$merge_error"
219 # Match the index to the working tree, and do a three-way.
220 git diff-files
--name-only | git update-index
--remove --stdin &&
221 work
=`git write-tree` &&
222 git read-tree
$v --reset -u $new ||
exit
224 eval GITHEAD_
$new='${new_name:-${branch:-$new}}' &&
225 eval GITHEAD_
$work=local &&
226 export GITHEAD_
$new GITHEAD_
$work &&
227 git merge-recursive
$old -- $new $work
229 # Do not register the cleanly merged paths in the index yet.
230 # this is not a real merge before committing, but just carrying
231 # the working tree changes along.
232 unmerged
=`git ls-files -u`
233 git read-tree
$v --reset $new
238 z40
=0000000000000000000000000000000000000000
240 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
242 ) | git update-index
--index-info
248 if test "$saved_err" = 0 && test -z "$quiet"
250 git diff-index
--name-status "$new"
256 # Switch the HEAD pointer to the new branch if we
257 # checked out a branch head, and remove any potential
258 # old MERGE_HEAD's (subsequent commits will clearly not
259 # be based on them, since we re-set the index)
261 if [ "$?" -eq 0 ]; then
262 if [ "$newbranch" ]; then
263 git branch
$track $newbranch_log "$newbranch" "$new_name" ||
exit
268 old_branch_name
=`expr "z$oldbranch" : 'zrefs/heads/\(.*\)'`
269 GIT_DIR
="$GIT_DIR" git symbolic-ref
-m "checkout: moving from ${old_branch_name:-$old} to $branch" HEAD
"refs/heads/$branch"
273 elif test "refs/heads/$branch" = "$oldbranch"
275 echo >&2 "Already on branch \"$branch\""
277 echo >&2 "Switched to${newbranch:+ a new} branch \"$branch\""
279 elif test -n "$detached"
281 old_branch_name
=`expr "z$oldbranch" : 'zrefs/heads/\(.*\)'`
282 git update-ref
--no-deref -m "checkout: moving from ${old_branch_name:-$old} to $arg" HEAD
"$detached" ||
283 die
"Cannot detach HEAD"
284 if test -n "$detach_warn"
286 echo >&2 "$detach_warn"
288 describe_detached_head
'HEAD is now at' HEAD
290 rm -f "$GIT_DIR/MERGE_HEAD"
295 # Run a post-checkout hook
296 if test -x "$GIT_DIR"/hooks
/post-checkout
; then
297 "$GIT_DIR"/hooks
/post-checkout
$old $new 1