3 USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
7 old=$(git-rev-parse HEAD)
16 while [ "$#" != "0" ]; do
23 [ -z "$newbranch" ] &&
24 die "git checkout: -b needs a branch name"
25 [ -e "$GIT_DIR/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 [ -f "$GIT_DIR/refs/heads/$arg" ]; then
57 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
59 # checking out selected paths from a tree-ish.
61 new_name="$arg^{tree}"
79 # The behaviour of the command with and without explicit path
80 # parameters is quite different.
82 # Without paths, we are checking out everything in the work tree,
83 # possibly switching branches. This is the traditional behaviour.
85 # With paths, we are _never_ switching branch, but checking out
86 # the named paths from either index (when no rev is given),
87 # or the named tree-ish (when rev is given).
95 Did you intend to checkout '$@' which can not be resolved as commit?"
97 if test '' != "$newbranch$force$merge"
99 die "git checkout: updating paths is incompatible with switching branches/forcing$hint"
103 # from a specific tree-ish; note that this is for
104 # rescuing paths and is never meant to remove what
105 # is not in the named tree-ish.
106 git-ls-tree --full-name -r "$new" "$@" |
107 git-update-index --index-info || exit $?
109 git-checkout-index -f -u -- "$@"
112 # Make sure we did not fall back on $arg^{tree} codepath
113 # since we are not checking out from an arbitrary tree-ish,
114 # but switching branches.
117 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
118 die "Cannot switch branch to a non-commit."
122 # We are switching branches and checking out trees, so
123 # we *NEED* to be at the toplevel.
124 cdup=$(git-rev-parse --show-cdup)
130 [ -z "$new" ] && new=$old && new_name="$old_name"
132 # If we don't have an old branch that we're switching to,
133 # and we don't have a new branch name for the target we
134 # are switching to, then we'd better just be checking out
135 # what we already had
137 [ -z "$branch$newbranch" ] &&
138 [ "$new" != "$old" ] &&
139 die "git checkout: to checkout the requested commit you need to specify
140 a name for a new branch which is created and switched to"
144 git-read-tree --reset -u $new
146 git-update-index --refresh >/dev/null
147 merge_error=$(git-read-tree -m -u $old $new 2>&1) || (
150 echo >&2 "$merge_error"
154 # Match the index to the working tree, and do a three-way.
155 git diff-files --name-only | git update-index --remove --stdin &&
156 work=`git write-tree` &&
157 git read-tree --reset -u $new &&
158 git read-tree -m -u --aggressive $old $new $work || exit
160 if result=`git write-tree 2>/dev/null`
162 echo >&2 "Trivially automerged."
164 git merge-index -o git-merge-one-file -a
167 # Do not register the cleanly merged paths in the index yet.
168 # this is not a real merge before committing, but just carrying
169 # the working tree changes along.
170 unmerged=`git ls-files -u`
171 git read-tree --reset $new
176 z40=0000000000000000000000000000000000000000
178 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
180 ) | git update-index --index-info
186 if test "$saved_err" = 0
188 test "$new" = "$old" || git diff-index --name-status "$new"
194 # Switch the HEAD pointer to the new branch if we
195 # checked out a branch head, and remove any potential
196 # old MERGE_HEAD's (subsequent commits will clearly not
197 # be based on them, since we re-set the index)
199 if [ "$?" -eq 0 ]; then
200 if [ "$newbranch" ]; then
201 if [ "$newbranch_log" ]; then
202 mkdir -p $(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
203 touch "$GIT_DIR/logs/refs/heads/$newbranch"
205 git-update-ref -m "checkout: Created from $new_name" "refs/heads/$newbranch" $new || exit
209 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
210 rm -f "$GIT_DIR/MERGE_HEAD"