git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH] Detached HEAD (experimental)

From
Junio C Hamano <junkio@cox.net>
Date
Jan 2, 2007, 07:45 UTC
Message-ID
<7vac11yirf.fsf@assigned-by-dhcp.cox.net>

This allows "git checkout -d v1.4.3" to detach the HEAD from any branch but point directly at the named commit. After this, "git branch" starts reporting that you are not on any branch. You can merge into "current branch" although there is not even such a thing.

You can go back the normal state by switching to an existing branch, say, "git checkout master" for example. Another way to get out of this is "git checkout -b newbranch".

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 and know what you are doing. 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.

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. We'll see.

Signed-off-by: Junio C Hamano <junkio@cox.net>
---
 builtin-branch.c |   36 ++++++++++++++++++++++++++----------
 cache.h          |    2 +-
 git-checkout.sh  |   22 +++++++++++++++++++---
 path.c           |   26 ++++++++++++++++++--------
 setup.c          |    5 +++--
 5 files changed, 67 insertions(+), 24 deletions(-)
diff --git a/builtin-branch.c b/builtin-branch.c
index 745ee04..71f88f2 100644
--- a/builtin-branch.c
+++ b/builtin-branch.c
@@ -299,7 +299,8 @@ static void print_ref_list(int kinds, int verbose, int abbrev)
 	free_ref_list(&ref_list);
 }
 
-static void create_branch(const char *name, const char *start,
+static void create_branch(const char *name, const char *start_name,
+			  unsigned char *start_sha1,
 			  int force, int reflog)
 {
 	struct ref_lock *lock;
@@ -318,9 +319,14 @@ static void create_branch(const char *name, const char *start,
 			die("Cannot force update the current branch.");
 	}
 
-	if (get_sha1(start, sha1) ||
-	    (commit = lookup_commit_reference(sha1)) == NULL)
-		die("Not a valid branch point: '%s'.", start);
+	if (start_sha1)
+		/* detached HEAD */
+		hashcpy(sha1, start_sha1);
+	else if (get_sha1(start_name, sha1))
+		die("Not a valid object name: '%s'.", start_name);
+
+	if ((commit = lookup_commit_reference(sha1)) == NULL)
+		die("Not a valid branch point: '%s'.", start_name);
 	hashcpy(sha1, commit->object.sha1);
 
 	lock = lock_any_ref_for_update(ref, NULL);
@@ -329,7 +335,8 @@ static void create_branch(const char *name, const char *start,
 
 	if (reflog) {
 		log_all_ref_updates = 1;
-		snprintf(msg, sizeof msg, "branch: Created from %s", start);
+		snprintf(msg, sizeof msg, "branch: Created from %s",
+			 start_name);
 	}
 
 	if (write_ref_sha1(lock, sha1, msg) < 0)
@@ -341,6 +348,9 @@ static void rename_branch(const char *oldname, const char *newname, int force)
 	char oldref[PATH_MAX], newref[PATH_MAX], logmsg[PATH_MAX*2 + 100];
 	unsigned char sha1[20];
 
+	if (!oldname)
+		die("cannot rename the curren branch while not on any.");
+
 	if (snprintf(oldref, sizeof(oldref), "refs/heads/%s", oldname) > sizeof(oldref))
 		die("Old branchname too long");
 
@@ -447,9 +457,15 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	head = xstrdup(resolve_ref("HEAD", head_sha1, 0, NULL));
 	if (!head)
 		die("Failed to resolve HEAD as a valid ref.");
-	if (strncmp(head, "refs/heads/", 11))
-		die("HEAD not found below refs/heads!");
-	head += 11;
+	if (!strcmp(head, "HEAD")) {
+		/* detached HEAD */
+		;
+	}
+	else {
+		if (strncmp(head, "refs/heads/", 11))
+			die("HEAD not found below refs/heads!");
+		head += 11;
+	}
 
 	if (delete)
 		return delete_branches(argc - i, argv + i, force_delete, kinds);
@@ -460,9 +476,9 @@ int cmd_branch(int argc, const char **argv, const char *prefix)
 	else if (rename && (i == argc - 2))
 		rename_branch(argv[i], argv[i + 1], force_rename);
 	else if (i == argc - 1)
-		create_branch(argv[i], head, force_create, reflog);
+		create_branch(argv[i], head, head_sha1, force_create, reflog);
 	else if (i == argc - 2)
-		create_branch(argv[i], argv[i + 1], force_create, reflog);
+		create_branch(argv[i], argv[i+1], NULL, force_create, reflog);
 	else
 		usage(builtin_branch_usage);
 
diff --git a/cache.h b/cache.h
index 29dd290..891045c 100644
--- a/cache.h
+++ b/cache.h
@@ -296,7 +296,7 @@ extern char *sha1_to_hex(const unsigned char *sha1);	/* static buffer result! */
 extern int read_ref(const char *filename, unsigned char *sha1);
 extern const char *resolve_ref(const char *path, unsigned char *sha1, int, int *);
 extern int create_symref(const char *ref, const char *refs_heads_master);
-extern int validate_symref(const char *ref);
+extern int validate_headref(const char *ref);
 
 extern int base_name_compare(const char *name1, int len1, int mode1, const char *name2, int len2, int mode2);
 extern int cache_name_compare(const char *name1, int len1, const char *name2, int len2);
diff --git a/git-checkout.sh b/git-checkout.sh
index 92ec069..c50df28 100755
--- a/git-checkout.sh
+++ b/git-checkout.sh
@@ -1,6 +1,6 @@
 #!/bin/sh
 
-USAGE='[-f] [-b <new_branch>] [-m] [<branch>] [<paths>...]'
+USAGE='[-f] [-b <new_branch>] [-d] [-m] [<branch>] [<paths>...]'
 SUBDIRECTORY_OK=Sometimes
 . git-sh-setup
 
@@ -12,6 +12,7 @@ force=
 branch=
 newbranch=
 newbranch_log=
+detached=
 merge=
 while [ "$#" != "0" ]; do
     arg="$1"
@@ -27,6 +28,9 @@ while [ "$#" != "0" ]; do
 		git-check-ref-format "heads/$newbranch" ||
 			die "git checkout: we do not like '$newbranch' as a branch name."
 		;;
+	-d)
+		detached=1
+		;;
 	"-l")
 		newbranch_log=1
 		;;
@@ -144,13 +148,25 @@ fi
 # are switching to, then we'd better just be checking out
 # what we already had
 
-[ -z "$branch$newbranch" ] &&
-	[ "$new" != "$old" ] &&
+if test -z "$branch$newbranch" && test "$new" != "$old"
+then
+	case "$detached" in
+	'')
 	die "git checkout: provided reference cannot be checked out directly
 
   You need -b to associate a new branch with the wanted checkout. Example:
   git checkout -b <new_branch_name> $arg
 "
+		;;
+	1)
+		# 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"
+		;;
+	esac
+fi
 
 if [ "X$old" = X ]
 then
diff --git a/path.c b/path.c
index 066f621..94ddd7e 100644
--- a/path.c
+++ b/path.c
@@ -90,10 +90,11 @@ int git_mkstemp(char *path, size_t len, const char *template)
 }
 
 
-int validate_symref(const char *path)
+int validate_headref(const char *path)
 {
 	struct stat st;
 	char *buf, buffer[256];
+	unsigned char sha1[20];
 	int len, fd;
 
 	if (lstat(path, &st) < 0)
@@ -119,14 +120,23 @@ int validate_symref(const char *path)
 	/*
 	 * Is it a symbolic ref?
 	 */
-	if (len < 4 || memcmp("ref:", buffer, 4))
+	if (len < 4)
 		return -1;
-	buf = buffer + 4;
-	len -= 4;
-	while (len && isspace(*buf))
-		buf++, len--;
-	if (len >= 5 && !memcmp("refs/", buf, 5))
+	if (!memcmp("ref:", buffer, 4)) {
+		buf = buffer + 4;
+		len -= 4;
+		while (len && isspace(*buf))
+			buf++, len--;
+		if (len >= 5 && !memcmp("refs/", buf, 5))
+			return 0;
+	}
+
+	/*
+	 * Is this a detached HEAD?
+	 */
+	if (!get_sha1_hex(buffer, sha1))
 		return 0;
+
 	return -1;
 }
 
@@ -241,7 +251,7 @@ char *enter_repo(char *path, int strict)
 		return NULL;
 
 	if (access("objects", X_OK) == 0 && access("refs", X_OK) == 0 &&
-	    validate_symref("HEAD") == 0) {
+	    validate_headref("HEAD") == 0) {
 		putenv("GIT_DIR=.");
 		check_repository_format();
 		return path;
diff --git a/setup.c b/setup.c
index 2ae57f7..cc97f9f 100644
--- a/setup.c
+++ b/setup.c
@@ -138,7 +138,8 @@ const char **get_pathspec(const char *prefix, const char **pathspec)
  *    GIT_OBJECT_DIRECTORY environment variable
  *  - a refs/ directory
  *  - either a HEAD symlink or a HEAD file that is formatted as
- *    a proper "ref:".
+ *    a proper "ref:", or a regular file HEAD that has a properly
+ *    formatted sha1 object name.
  */
 static int is_git_directory(const char *suspect)
 {
@@ -161,7 +162,7 @@ static int is_git_directory(const char *suspect)
 		return 0;
 
 	strcpy(path + len, "/HEAD");
-	if (validate_symref(path))
+	if (validate_headref(path))
 		return 0;
 
 	return 1;
-- 
1.5.0.rc0.gab5a
Next: Edgar Toernig
Message 1 of 68 in “Detached HEAD (experimental)”
  1. Detached HEAD (experimental)Junio C Hamano, Jan 2, 2007
  2. Edgar ToernigJan 2, 2007
  3. Carl WorthJan 2, 2007
  4. Jakub NarebskiJan 2, 2007
  5. Carl WorthJan 3, 2007
  6. J. Bruce FieldsJan 6, 2007
  7. Alan ChandlerJan 6, 2007
  8. J. Bruce FieldsJan 6, 2007
  9. Junio C HamanoJan 2, 2007
  10. Carl WorthJan 2, 2007
  11. Junio C HamanoJan 3, 2007
  12. Junio C HamanoJan 8, 2007
  13. Jeff KingJan 8, 2007
  14. Junio C HamanoJan 9, 2007
  15. Carl WorthJan 9, 2007
  16. Junio C HamanoJan 9, 2007
  17. Carl WorthJan 9, 2007
  18. Shawn O. PearceJan 9, 2007
  19. Junio C HamanoJan 9, 2007
  20. 0/6 Expose in_merge_bases() via merge-base.Junio C Hamano, Jan 9, 2007
  21. Luben TuikovJan 9, 2007
  22. Jeff KingJan 9, 2007
  23. Junio C HamanoJan 9, 2007
  24. J. Bruce FieldsJan 9, 2007
  25. Carl WorthJan 9, 2007
  26. J. Bruce FieldsJan 9, 2007
  27. Shawn O. PearceJan 9, 2007
  28. Jakub NarebskiJan 10, 2007
  29. Shawn O. PearceJan 10, 2007
  30. J. Bruce FieldsJan 10, 2007
  31. Shawn O. PearceJan 10, 2007
  32. Nicolas PitreJan 10, 2007
  33. Junio C HamanoJan 10, 2007
  34. Shawn O. PearceJan 10, 2007
  35. Nicolas PitreJan 10, 2007
  36. Shawn O. PearceJan 10, 2007
  37. Jakub NarebskiJan 10, 2007
  38. Andreas EricssonJan 10, 2007
  39. Junio C HamanoJan 10, 2007
  40. Daniel BarkalowJan 10, 2007
  41. Andreas EricssonJan 11, 2007
  42. Junio C HamanoJan 10, 2007
  43. Junio C HamanoJan 9, 2007
  44. Shawn O. PearceJan 9, 2007
  45. Linus TorvaldsJan 9, 2007
  46. Junio C HamanoJan 10, 2007
  47. Shawn O. PearceJan 10, 2007
  48. Linus TorvaldsJan 10, 2007
  49. Carl WorthJan 10, 2007
  50. Junio C HamanoJan 10, 2007
  51. Andy ParkinsJan 10, 2007
  52. Shawn O. PearceJan 10, 2007
  53. Junio C HamanoJan 10, 2007
  54. Andy ParkinsJan 10, 2007
  55. Shawn O. PearceJan 10, 2007
  56. Junio C HamanoJan 10, 2007
  57. Jeff KingJan 10, 2007
  58. Junio C HamanoJan 11, 2007
  59. J. Bruce FieldsJan 11, 2007
  60. Jeff KingJan 3, 2007
  61. Jeff KingJan 3, 2007
  62. git-branch: show detached HEADLars Hjemli, Jan 2, 2007
  63. Shawn O. PearceJan 3, 2007
  64. Junio C HamanoJan 3, 2007
  65. Lars HjemliJan 3, 2007
  66. Junio C HamanoJan 3, 2007
  67. Junio C HamanoJan 3, 2007
  68. Lars HjemliJan 3, 2007

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.