3 USAGE
='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
4 SUBDIRECTORY_OK
=Sometimes
8 old
=$
(git-rev-parse
--verify $old_name 2>/dev
/null
)
9 oldbranch
=$
(git-symbolic-ref
$old_name 2>/dev
/null
)
19 while [ "$#" != "0" ]; do
26 [ -z "$newbranch" ] &&
27 die
"git checkout: -b needs a branch name"
28 git-show-ref
--verify --quiet -- "refs/heads/$newbranch" &&
29 die
"git checkout: branch $newbranch already exists"
30 git-check-ref-format
"heads/$newbranch" ||
31 die
"git checkout: we do not like '$newbranch' as a branch name."
49 if rev=$
(git-rev-parse
--verify "$arg^0" 2>/dev
/null
)
51 if [ -z "$rev" ]; then
52 echo "unknown flag $arg"
57 if git-show-ref
--verify --quiet -- "refs/heads/$arg"
61 elif rev=$
(git-rev-parse
--verify "$arg^{tree}" 2>/dev
/null
)
63 # checking out selected paths from a tree-ish.
65 new_name
="$arg^{tree}"
83 case "$force$merge" in
85 die
"git checkout: -f and -m are incompatible"
88 # The behaviour of the command with and without explicit path
89 # parameters is quite different.
91 # Without paths, we are checking out everything in the work tree,
92 # possibly switching branches. This is the traditional behaviour.
94 # With paths, we are _never_ switching branch, but checking out
95 # the named paths from either index (when no rev is given),
96 # or the named tree-ish (when rev is given).
104 Did you intend to checkout '$@' which can not be resolved as commit?"
106 if test '' != "$newbranch$force$merge"
108 die
"git checkout: updating paths is incompatible with switching branches/forcing$hint"
112 # from a specific tree-ish; note that this is for
113 # rescuing paths and is never meant to remove what
114 # is not in the named tree-ish.
115 git-ls-tree
--full-name -r "$new" "$@" |
116 git-update-index
--index-info ||
exit $?
119 # Make sure the request is about existing paths.
120 git-ls-files
--error-unmatch -- "$@" >/dev
/null ||
exit
121 git-ls-files
-- "$@" |
122 git-checkout-index
-f -u --stdin
125 # Make sure we did not fall back on $arg^{tree} codepath
126 # since we are not checking out from an arbitrary tree-ish,
127 # but switching branches.
130 git-rev-parse
--verify "$new^{commit}" >/dev
/null
2>&1 ||
131 die
"Cannot switch branch to a non-commit."
135 # We are switching branches and checking out trees, so
136 # we *NEED* to be at the toplevel.
137 cdup
=$
(git-rev-parse
--show-cdup)
143 [ -z "$new" ] && new
=$old && new_name
="$old_name"
145 # If we don't have an existing branch that we're switching to,
146 # and we don't have a new branch name for the target we
147 # are switching to, then we are detaching our HEAD from any
148 # branch. However, if "git checkout HEAD" detaches the HEAD
149 # from the current branch, even though that may be logically
150 # correct, it feels somewhat funny. More importantly, we do not
151 # want "git checkout" nor "git checkout -f" to detach HEAD.
156 if test -z "$branch$newbranch" && test "$new" != "$old"
159 if test -n "$oldbranch"
161 detach_warn
="warning: you are not on ANY branch anymore.
162 If you meant to create a new branch from the commit, you need -b to
163 associate a new branch with the wanted checkout. Example:
164 git checkout -b <new_branch_name> $arg"
166 elif test -z "$oldbranch" && test -n "$branch"
171 git show-ref
-d -s |
grep "$old" >/dev
/null ||
{
173 "You are not on any branch and switching to branch '$new_name'
174 may lose your changes. At this point, you can do one of two things:
175 (1) Decide it is Ok and say 'git checkout -f $new_name';
176 (2) Start a new branch from the current commit, by saying
177 'git checkout -b <branch-name>'.
178 Leaving your HEAD detached; not switching to branch '$new_name'."
186 echo >&2 "warning: You appear to be on a branch yet to be born."
187 echo >&2 "warning: Forcing checkout of $new_name."
193 git-read-tree
--reset -u $new
195 git-update-index
--refresh >/dev
/null
196 merge_error
=$
(git-read-tree
-m -u --exclude-per-directory=.gitignore
$old $new 2>&1) ||
(
199 echo >&2 "$merge_error"
203 # Match the index to the working tree, and do a three-way.
204 git diff-files
--name-only | git update-index
--remove --stdin &&
205 work
=`git write-tree` &&
206 git read-tree
--reset -u $new &&
207 git read-tree
-m -u --aggressive --exclude-per-directory=.gitignore
$old $new $work ||
210 if result
=`git write-tree 2>/dev/null`
212 echo >&2 "Trivially automerged."
214 git merge-index
-o git-merge-one-file
-a
217 # Do not register the cleanly merged paths in the index yet.
218 # this is not a real merge before committing, but just carrying
219 # the working tree changes along.
220 unmerged
=`git ls-files -u`
221 git read-tree
--reset $new
226 z40
=0000000000000000000000000000000000000000
228 sed -e 's/^[0-7]* [0-9a-f]* /'"0 $z40 /"
230 ) | git update-index
--index-info
236 if test "$saved_err" = 0
238 test "$new" = "$old" || git diff-index
--name-status "$new"
244 # Switch the HEAD pointer to the new branch if we
245 # checked out a branch head, and remove any potential
246 # old MERGE_HEAD's (subsequent commits will clearly not
247 # be based on them, since we re-set the index)
249 if [ "$?" -eq 0 ]; then
250 if [ "$newbranch" ]; then
251 if [ "$newbranch_log" ]; then
252 mkdir
-p $
(dirname "$GIT_DIR/logs/refs/heads/$newbranch")
253 touch "$GIT_DIR/logs/refs/heads/$newbranch"
255 git-update-ref
-m "checkout: Created from $new_name" "refs/heads/$newbranch" $new ||
exit
260 GIT_DIR
="$GIT_DIR" git-symbolic-ref HEAD
"refs/heads/$branch"
261 elif test -n "$detached"
263 # NEEDSWORK: we would want a command to detach the HEAD
264 # atomically, instead of this handcrafted command sequence.
266 # git update-ref --detach HEAD $new
267 # or something like that...
269 echo "$detached" >"$GIT_DIR/HEAD.new" &&
270 mv "$GIT_DIR/HEAD.new" "$GIT_DIR/HEAD" ||
271 die
"Cannot detach HEAD"
272 if test -n "$detach_warn"
274 echo >&2 "$detach_warn"
277 rm -f "$GIT_DIR/MERGE_HEAD"