3 USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK=Sometimes
7 old=$(git-rev-parse HEAD)
12 while [ "$#" != "0" ]; do
19 [ -z "$newbranch" ] &&
20 die "git checkout: -b needs a branch name"
21 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
22 die "git checkout: branch $newbranch already exists"
23 git-check-ref-format "heads/$newbranch" ||
24 die "we do not like '$newbranch' as a branch name."
36 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
38 if [ -z "$rev" ]; then
39 echo "unknown flag $arg"
43 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
46 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
48 # checking out selected paths from a tree-ish.
62 # The behaviour of the command with and without explicit path
63 # parameters is quite different.
65 # Without paths, we are checking out everything in the work tree,
66 # possibly switching branches. This is the traditional behaviour.
68 # With paths, we are _never_ switching branch, but checking out
69 # the named paths from either index (when no rev is given),
70 # or the named tree-ish (when rev is given).
74 if test '' != "$newbranch$force"
76 die "updating paths and switching branches or forcing are incompatible."
80 # from a specific tree-ish; note that this is for
81 # rescuing paths and is never meant to remove what
82 # is not in the named tree-ish.
83 git-ls-tree --full-name -r "$new" "$@" |
84 git-update-index --index-info || exit $?
86 git-checkout-index -f -u -- "$@"
89 # Make sure we did not fall back on $arg^{tree} codepath
90 # since we are not checking out from an arbitrary tree-ish,
91 # but switching branches.
94 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
95 die "Cannot switch branch to a non-commit."
99 # We are switching branches and checking out trees, so
100 # we *NEED* to be at the toplevel.
101 cdup=$(git-rev-parse --show-cdup)
107 [ -z "$new" ] && new=$old
109 # If we don't have an old branch that we're switching to,
110 # and we don't have a new branch name for the target we
111 # are switching to, then we'd better just be checking out
112 # what we already had
114 [ -z "$branch$newbranch" ] &&
115 [ "$new" != "$old" ] &&
116 die "git checkout: you need to specify a new branch name"
120 git-read-tree --reset $new &&
121 git-checkout-index -q -f -u -a
123 git-update-index --refresh >/dev/null
124 git-read-tree -m -u $old $new
128 # Switch the HEAD pointer to the new branch if we
129 # checked out a branch head, and remove any potential
130 # old MERGE_HEAD's (subsequent commits will clearly not
131 # be based on them, since we re-set the index)
133 if [ "$?" -eq 0 ]; then
134 if [ "$newbranch" ]; then
135 leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
136 mkdir -p "$GIT_DIR/$leading" &&
137 echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
141 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
142 rm -f "$GIT_DIR/MERGE_HEAD"