path: root/
diff options
authorJunio C Hamano <>2007-01-02 07:31:08 (GMT)
committerJunio C Hamano <>2007-01-08 11:02:11 (GMT)
commitc847f537125ceab3425205721fdaaa834e6d8a83 (patch)
treeff9d242fe9d7bd4008f16d7f43e860e0db861981 /
parent0016a48251abefed11efc919703d980a21c95f2c (diff)
Detached HEAD (experimental)
This allows "git checkout v1.4.3" to dissociate the HEAD of repository from any branch. After this point, "git branch" starts reporting that you are not on any branch. You can go back to an existing branch by saying "git checkout master", for example. This is still experimental. While I think it makes sense to allow commits on top of detached HEAD, it is rather dangerous unless you are careful in the current form. Next "git checkout master" will obviously lose what you have done, so we might want to require "git checkout -f" out of a detached HEAD if we find that the HEAD commit is not an ancestor of any other branches. There is no such safety valve implemented right now. On the other hand, the reason the user did not start the ad-hoc work on a new branch with "git checkout -b" was probably because the work was of a throw-away nature, so the convenience of not having that safety valve might be even better. The user, after accumulating some commits on top of a detached HEAD, can always create a new branch with "git checkout -b" not to lose useful work done while the HEAD was detached. We'll see. Signed-off-by: Junio C Hamano <>
Diffstat (limited to '')
1 files changed, 11 insertions, 5 deletions
diff --git a/ b/
index 92ec069..8e11ca4 100755
--- a/
+++ b/
@@ -144,13 +144,19 @@ fi
# are switching to, then we'd better just be checking out
# what we already had
-[ -z "$branch$newbranch" ] &&
- [ "$new" != "$old" ] &&
- die "git checkout: provided reference cannot be checked out directly
- You need -b to associate a new branch with the wanted checkout. Example:
+if test -z "$branch$newbranch" && test "$new" != "$old"
+ # NEEDSWORK: we would want to have this command here
+ # that allows us to detach the HEAD atomically.
+ # git update-ref --detach HEAD "$new"
+ rm -f "$GIT_DIR/HEAD"
+ echo "$new" >"$GIT_DIR/HEAD"
+ echo >&2 "WARNING: you are not on ANY branch anymore.
+If you meant to create a new branch from the commit, you need -b to
+associate a new branch with the wanted checkout. Example:
git checkout -b <new_branch_name> $arg
if [ "X$old" = X ]