3 USAGE
='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK
=Sometimes
8 old
=$
(git-rev-parse
--verify $old_name 2>/dev
/null
)
16 while [ "$#" != "0" ]; do
23 [ -z "$newbranch" ] &&
24 die
"git checkout: -b needs a branch name"
25 git-show-ref
--verify --quiet -- "refs/heads/$newbranch" &&
26 die
"git checkout: branch $newbranch already exists"
27 git-check-ref-format
"heads/$newbranch" ||
28 die
"git checkout: we do not like '$newbranch' as a branch name."
46 if rev=$
(git-rev-parse
--verify "$arg^0" 2>/dev
/null
)
48 if [ -z "$rev" ]; then
49 echo "unknown flag $arg"
54 if git-show-ref
--verify --quiet -- "refs/heads/$arg"
58 elif rev=$
(git-rev-parse
--verify "$arg^{tree}" 2>/dev
/null
)
60 # checking out selected paths from a tree-ish.
62 new_name
="$arg^{tree}"
80 case "$force$merge" in
82 die
"git checkout: -f and -m are incompatible"
85 # The behaviour of the command with and without explicit path
86 # parameters is quite different.
88 # Without paths, we are checking out everything in the work tree,
89 # possibly switching branches. This is the traditional behaviour.
91 # With paths, we are _never_ switching branch, but checking out
92 # the named paths from either index (when no rev is given),
93 # or the named tree-ish (when rev is given).
101 Did you intend to checkout '$@' which can not be resolved as commit?"
103 if test '' != "$newbranch$force$merge"
105 die
"git checkout: updating paths is incompatible with switching branches/forcing$hint"
109 # from a specific tree-ish; note that this is for
110 # rescuing paths and is never meant to remove what
111 # is not in the named tree-ish.
112 git-ls-tree
--full-name -r "$new" "$@" |
113 git-update-index
--index-info ||
exit $?
116 # Make sure the request is about existing paths.
117 git-ls-files
--error-unmatch -- "$@" >/dev
/null ||
exit
118 git-ls-files
-- "$@" |
119 git-checkout-index
-f -u --stdin
122 # Make sure we did not fall back on $arg^{tree} codepath
123 # since we are not checking out from an arbitrary tree-ish,
124 # but switching branches.
127 git-rev-parse
--verify "$new^{commit}" >/dev
/null
2>&1 ||
128 die
"Cannot switch branch to a non-commit."
132 # We are switching branches and checking out trees, so
133 # we *NEED* to be at the toplevel.
134 cdup
=$
(git-rev-parse
--show-cdup)
140 [ -z "$new" ] && new
=$old && new_name
="$old_name"
142 # If we don't have an old branch that we're switching to,
143 # and we don't have a new branch name for the target we
144 # are switching to, then we'd better just be checking out
145 # what we already had
147 [ -z "$branch$newbranch" ] &&
148 [ "$new" != "$old" ] &&
149 die
"git checkout: provided reference cannot be checked out directly
151 You need -b to associate a new branch with the wanted checkout. Example:
152 git checkout -b <new_branch_name> $arg
157 echo "warning: You do not appear to currently be on a branch." >&2
158 echo "warning: Forcing checkout of $new_name." >&2
164 git-read-tree
--reset -u $new
166 git-update-index
--refresh >/dev
/null
167 merge_error
=$
(git-read-tree
-m -u --exclude-per-directory=.gitignore
$old $new 2>&1) ||
(
170 echo >&2 "$merge_error"
174 # Match the index to the working tree, and do a three-way.
175 git diff-files
--name-only | git update-index
--remove --stdin &&
176 work
=`git write-tree` &&
177 git read-tree
--reset -u $new &&
178 git read-tree
-m -u --aggressive --exclude-per-directory=.gitignore
$old $new $work ||
181 if result
=`git write-tree 2>/dev/null`
183 echo >&2 "Trivially automerged."
185 git merge-index
-o git-merge-one-file
-a
188 # Do not register the cleanly merged paths in the index yet.
189 # this is not a real merge before committing, but just carrying
190 # the working tree changes along.
191 unmerged
=`git ls-files -u`
192 git read-tree
--reset $new
197 z40
=0000000000000000000000000000000000000000
199 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
201 ) | git update-index
--index-info
207 if test "$saved_err" = 0
209 test "$new" = "$old" || git diff-index
--name-status "$new"
215 # Switch the HEAD pointer to the new branch if we
216 # checked out a branch head, and remove any potential
217 # old MERGE_HEAD's (subsequent commits will clearly not
218 # be based on them, since we re-set the index)
220 if [ "$?" -eq 0 ]; then
221 if [ "$newbranch" ]; then
222 if [ "$newbranch_log" ]; then
223 mkdir
-p $
(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
224 touch "$GIT_DIR/logs/refs/heads/$newbranch"
226 git-update-ref
-m "checkout: Created from $new_name" "refs/heads/$newbranch" $new ||
exit
230 GIT_DIR
="$GIT_DIR" git-symbolic-ref HEAD
"refs/heads/$branch"
231 rm -f "$GIT_DIR/MERGE_HEAD"