3 USAGE='[-f] [-b <new_branch>] [<branch>] [<paths>...]'
6 old=$(git-rev-parse HEAD)
11 while [ "$#" != "0" ]; do
18 [ -z "$newbranch" ] &&
19 die "git checkout: -b needs a branch name"
20 [ -e "$GIT_DIR/refs/heads/$newbranch" ] &&
21 die "git checkout: branch $newbranch already exists"
22 git-check-ref-format "heads/$newbranch" ||
23 die "we do not like '$newbranch' as a branch name."
35 if rev=$(git-rev-parse --verify "$arg^0" 2>/dev/null)
37 if [ -z "$rev" ]; then
38 echo "unknown flag $arg"
42 if [ -f "$GIT_DIR/refs/heads/$arg" ]; then
45 elif rev=$(git-rev-parse --verify "$arg^{tree}" 2>/dev/null)
47 # checking out selected paths from a tree-ish.
61 # The behaviour of the command with and without explicit path
62 # parameters is quite different.
64 # Without paths, we are checking out everything in the work tree,
65 # possibly switching branches. This is the traditional behaviour.
67 # With paths, we are _never_ switching branch, but checking out
68 # the named paths from either index (when no rev is given),
69 # or the named tree-ish (when rev is given).
73 if test '' != "$newbranch$force"
75 die "updating paths and switching branches or forcing are incompatible."
79 # from a specific tree-ish; note that this is for
80 # rescuing paths and is never meant to remove what
81 # is not in the named tree-ish.
82 git-ls-tree -r "$new" "$@" |
83 git-update-index --index-info || exit $?
85 git-checkout-index -f -u -- "$@"
88 # Make sure we did not fall back on $arg^{tree} codepath
89 # since we are not checking out from an arbitrary tree-ish,
90 # but switching branches.
93 git-rev-parse --verify "$new^{commit}" >/dev/null 2>&1 ||
94 die "Cannot switch branch to a non-commit."
98 [ -z "$new" ] && new=$old
100 # If we don't have an old branch that we're switching to,
101 # and we don't have a new branch name for the target we
102 # are switching to, then we'd better just be checking out
103 # what we already had
105 [ -z "$branch$newbranch" ] &&
106 [ "$new" != "$old" ] &&
107 die "git checkout: you need to specify a new branch name"
111 git-read-tree --reset $new &&
112 git-checkout-index -q -f -u -a
114 git-update-index --refresh >/dev/null
115 git-read-tree -m -u $old $new
119 # Switch the HEAD pointer to the new branch if we
120 # checked out a branch head, and remove any potential
121 # old MERGE_HEAD's (subsequent commits will clearly not
122 # be based on them, since we re-set the index)
124 if [ "$?" -eq 0 ]; then
125 if [ "$newbranch" ]; then
126 leading=`expr "refs/heads/$newbranch" : '\(.*\)/'` &&
127 mkdir -p "$GIT_DIR/$leading" &&
128 echo $new >"$GIT_DIR/refs/heads/$newbranch" || exit
132 GIT_DIR="$GIT_DIR" git-symbolic-ref HEAD "refs/heads/$branch"
133 rm -f "$GIT_DIR/MERGE_HEAD"