3 USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
7 old=$(git-rev-parse HEAD)
13 while [ "$#" != "0" ]; do
20 [ -z "$newbranch" ] &&
21 die "git checkout: -b needs a branch name"
22 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
23 die "git checkout: branch $newbranch already exists"
24 git-check-ref-format "heads/$newbranch" ||
25 die "we do not like '$newbranch' as a branch name."
40 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
42 if [ -z "$rev" ]; then
43 echo "unknown flag $arg"
47 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
50 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
52 # checking out selected paths from a tree-ish.
66 # The behaviour of the command with and without explicit path
67 # parameters is quite different.
69 # Without paths, we are checking out everything in the work tree,
70 # possibly switching branches. This is the traditional behaviour.
72 # With paths, we are _never_ switching branch, but checking out
73 # the named paths from either index (when no rev is given),
74 # or the named tree-ish (when rev is given).
78 if test '' != "$newbranch$force$merge"
80 die "updating paths and switching branches or forcing are incompatible."
84 # from a specific tree-ish; note that this is for
85 # rescuing paths and is never meant to remove what
86 # is not in the named tree-ish.
87 git-ls-tree --full-name -r "$new" "$@" |
88 git-update-index --index-info || exit $?
90 git-checkout-index -f -u -- "$@"
93 # Make sure we did not fall back on $arg^{tree} codepath
94 # since we are not checking out from an arbitrary tree-ish,
95 # but switching branches.
98 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
99 die "Cannot switch branch to a non-commit."
103 # We are switching branches and checking out trees, so
104 # we *NEED* to be at the toplevel.
105 cdup=$(git-rev-parse --show-cdup)
111 [ -z "$new" ] && new=$old
113 # If we don't have an old branch that we're switching to,
114 # and we don't have a new branch name for the target we
115 # are switching to, then we'd better just be checking out
116 # what we already had
118 [ -z "$branch$newbranch" ] &&
119 [ "$new" != "$old" ] &&
120 die "git checkout: you need to specify a new branch name"
124 git-read-tree --reset $new &&
125 git-checkout-index -q -f -u -a
127 git-update-index --refresh >/dev/null
128 merge_error=$(git-read-tree -m -u $old $new 2>&1) || (
131 echo >&2 "$merge_error"
135 # Match the index to the working tree, and do a three-way.
136 git diff-files --name-only | git update-index --remove --stdin &&
137 work=`git write-tree` &&
138 git read-tree --reset $new &&
139 git checkout-index -f -u -q -a &&
140 git read-tree -m -u $old $new $work || exit
142 if result=`git write-tree 2>/dev/null`
144 echo >&2 "Trivially automerged."
146 git merge-index -o git-merge-one-file -a
149 # Do not register the cleanly merged paths in the index yet.
150 # this is not a real merge before committing, but just carrying
151 # the working tree changes along.
152 unmerged=`git ls-files -u`
153 git read-tree --reset $new
158 z40=0000000000000000000000000000000000000000
160 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
162 ) | git update-index --index-info
168 if test "$saved_err" = 0
170 test "$new" = "$old" || git diff-index --name-status "$new"
176 # Switch the HEAD pointer to the new branch if we
177 # checked out a branch head, and remove any potential
178 # old MERGE_HEAD's (subsequent commits will clearly not
179 # be based on them, since we re-set the index)
181 if [ "$?" -eq 0 ]; then
182 if [ "$newbranch" ]; then
183 leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
184 mkdir -p "$GIT_DIR/$leading" &&
185 echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
189 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
190 rm -f "$GIT_DIR/MERGE_HEAD"