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

[PATCH 18/19] reset: allow reset on unborn branch

From
Martin von Zweigbergk <martinvonz@gmail.com>
Date
Jan 9, 2013, 08:16 UTC
Message-ID
<1357719376-16406-19-git-send-email-martinvonz@gmail.com>
In-Reply-To
<1357719376-16406-1-git-send-email-martinvonz@gmail.com>

Some users seem to think, knowingly or not, that being on an unborn branch is like having a commit with an empty tree checked out, but when run on an unborn branch, "git reset" currently fails with:

  fatal: Failed to resolve 'HEAD' as a valid ref.
Instead of making users figure out that they should run
 git rm --cached -r .
, let's teach "git reset" without a revision argument, when on an
unborn branch, to behave as if the user asked to reset to an empty
tree. Don't take the analogy with an empty commit too far, though, but
still disallow explictly referring to HEAD in "git reset HEAD".
---
 builtin/reset.c                | 17 ++++++++------
 t/t7106-reset-unborn-branch.sh | 52 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+), 7 deletions(-)
 create mode 100755 t/t7106-reset-unborn-branch.sh
diff --git a/builtin/reset.c b/builtin/reset.c
index 4c223bd..5cd48ac 100644
--- a/builtin/reset.c
+++ b/builtin/reset.c
@@ -239,7 +239,7 @@ static int update_refs(const char *rev, const unsigned char *sha1) {
 int cmd_reset(int argc, const char **argv, const char *prefix)
 {
 	int reset_type = NONE, update_ref_status = 0, quiet = 0;
-	int patch_mode = 0;
+	int patch_mode = 0, unborn;
 	const char *rev;
 	unsigned char sha1[20];
 	const char **pathspec = NULL;
@@ -264,8 +264,11 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 	argc = parse_options(argc, argv, prefix, options, git_reset_usage,
 						PARSE_OPT_KEEP_DASHDASH);
 	pathspec = parse_args(argc, argv, prefix, &rev);
-
-	if (!pathspec) {
+	unborn = !strcmp(rev, "HEAD") && get_sha1("HEAD", sha1);
+	if (unborn) {
+		/* reset on unborn branch: treat as reset to empty tree */
+		hashcpy(sha1, EMPTY_TREE_SHA1_BIN);
+	} else if (!pathspec) {
 		if (get_sha1_committish(rev, sha1))
 			die(_("Failed to resolve '%s' as a valid revision."), rev);
 		commit = lookup_commit_reference(sha1);
@@ -285,7 +288,7 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 	if (patch_mode) {
 		if (reset_type != NONE)
 			die(_("--patch is incompatible with --{hard,mixed,soft}"));
-		return run_add_interactive(rev, "--patch=reset", pathspec);
+		return run_add_interactive(sha1_to_hex(sha1), "--patch=reset", pathspec);
 	}
 
 	/* git reset tree [--] paths... can be used to
@@ -337,16 +340,16 @@ int cmd_reset(int argc, const char **argv, const char *prefix)
 			die(_("Could not write new index file."));
 	}
 
-	if (!pathspec) {
+	if (!pathspec && !unborn) {
 		/* Any resets without paths update HEAD to the head being
 		 * switched to, saving the previous head in ORIG_HEAD before. */
 		update_ref_status = update_refs(rev, sha1);
 
 		if (reset_type == HARD && !update_ref_status && !quiet)
 			print_new_head_line(commit);
-
-		remove_branch_state();
 	}
+	if (!pathspec)
+		remove_branch_state();
 
 	return update_ref_status;
 }
diff --git a/t/t7106-reset-unborn-branch.sh b/t/t7106-reset-unborn-branch.sh
new file mode 100755
index 0000000..4ff6af4
--- /dev/null
+++ b/t/t7106-reset-unborn-branch.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+
+test_description='git reset should work on unborn branch'
+. ./test-lib.sh
+
+test_expect_success 'setup' '
+	echo a >a &&
+	echo b >b
+'
+
+test_expect_success 'reset' '
+	git add a b &&
+	git reset &&
+	test "$(git ls-files)" == ""
+'
+
+test_expect_success 'reset HEAD' '
+	rm .git/index &&
+	git add a b &&
+	test_must_fail git reset HEAD
+'
+
+test_expect_success 'reset $file' '
+	rm .git/index &&
+	git add a b &&
+	git reset a &&
+	test "$(git ls-files)" == "b"
+'
+
+test_expect_success 'reset -p' '
+	rm .git/index &&
+	git add a &&
+	echo y | git reset -p &&
+	test "$(git ls-files)" == ""
+'
+
+test_expect_success 'reset --soft is a no-op' '
+	rm .git/index &&
+	git add a &&
+	git reset --soft
+	test "$(git ls-files)" == "a"
+'
+
+test_expect_success 'reset --hard' '
+	rm .git/index &&
+	git add a &&
+	git reset --hard &&
+	test "$(git ls-files)" == "" &&
+	test_path_is_missing a
+'
+
+test_done
-- 
1.8.1.rc3.331.g1ef2165
Previous: Junio C HamanoNext: Martin von Zweigbergk
Message 43 of 68 in “reset improvements”
  1. 00/19 reset improvementsMartin von Zweigbergk, Jan 9, 2013
  2. 01/19 reset $pathspec: no need to discard indexMartin von Zweigbergk, Jan 9, 2013
  3. 02/19 reset $pathspec: exit with code 0 if successfulMartin von Zweigbergk, Jan 9, 2013
  4. 03/19 reset.c: pass pathspec around instead of (prefix, argv) pairMartin von Zweigbergk, Jan 9, 2013
  5. Matt KraaiJan 9, 2013
  6. Junio C HamanoJan 9, 2013
  7. Duy NguyenJan 10, 2013
  8. Junio C HamanoJan 10, 2013
  9. Duy NguyenJan 11, 2013
  10. 04/19 reset: don't allow "git reset -- $pathspec" in bare repoMartin von Zweigbergk, Jan 9, 2013
  11. Junio C HamanoJan 9, 2013
  12. Martin von ZweigbergkJan 10, 2013
  13. Junio C HamanoJan 10, 2013
  14. 05/19 reset.c: extract function for parsing argumentsMartin von Zweigbergk, Jan 9, 2013
  15. 06/19 reset.c: remove unnecessary variable 'i'Martin von Zweigbergk, Jan 9, 2013
  16. Junio C HamanoJan 9, 2013
  17. Martin von ZweigbergkJan 10, 2013
  18. 07/19 reset.c: extract function for updating {ORIG,}HEADMartin von Zweigbergk, Jan 9, 2013
  19. Matt KraaiJan 9, 2013
  20. 08/19 reset.c: share call to die_if_unmerged_cache()Martin von Zweigbergk, Jan 9, 2013
  21. Junio C HamanoJan 9, 2013
  22. Martin von ZweigbergkJan 10, 2013
  23. 09/19 reset.c: replace switch by if-elseMartin von Zweigbergk, Jan 9, 2013
  24. Junio C HamanoJan 9, 2013
  25. Martin von ZweigbergkJan 11, 2013
  26. Junio C HamanoJan 11, 2013
  27. 10/19 reset --keep: only write index file onceMartin von Zweigbergk, Jan 9, 2013
  28. Junio C HamanoJan 9, 2013
  29. 11/19 reset: avoid redundant error messageMartin von Zweigbergk, Jan 9, 2013
  30. 12/19 reset.c: move update_index_refresh() call out of read_from_tree()Martin von Zweigbergk, Jan 9, 2013
  31. 13/19 reset.c: move lock, write and commit out of update_index_refresh()Martin von Zweigbergk, Jan 9, 2013
  32. 14/19 reset [--mixed]: don't write index file twiceMartin von Zweigbergk, Jan 9, 2013
  33. 15/19 reset.c: finish entire cmd_reset() whether or not pathspec is givenMartin von Zweigbergk, Jan 9, 2013
  34. Junio C HamanoJan 9, 2013
  35. 16/19 reset [--mixed] --quiet: don't refresh indexMartin von Zweigbergk, Jan 9, 2013
  36. Jeff KingJan 9, 2013
  37. Martin von ZweigbergkJan 9, 2013
  38. Junio C HamanoJan 9, 2013
  39. Martin von ZweigbergkJan 9, 2013
  40. Junio C HamanoJan 9, 2013
  41. 17/19 reset $sha1 $pathspec: require $sha1 only to be treeishMartin von Zweigbergk, Jan 9, 2013
  42. Junio C HamanoJan 9, 2013
  43. 18/19 reset: allow reset on unborn branchMartin von Zweigbergk, Jan 9, 2013
  44. 19/19 reset [--mixed]: use diff-based reset whether or not pathspec was givenMartin von Zweigbergk, Jan 9, 2013
  45. Junio C HamanoJan 9, 2013
  46. 00/19 reset improvementsMartin von Zweigbergk, Jan 15, 2013
  47. 01/19 reset $pathspec: no need to discard indexMartin von Zweigbergk, Jan 15, 2013
  48. 02/19 reset $pathspec: exit with code 0 if successfulMartin von Zweigbergk, Jan 15, 2013
  49. 03/19 reset.c: pass pathspec around instead of (prefix, argv) pairMartin von Zweigbergk, Jan 15, 2013
  50. 04/19 reset: don't allow "git reset -- $pathspec" in bare repoMartin von Zweigbergk, Jan 15, 2013
  51. 05/19 reset.c: extract function for parsing argumentsMartin von Zweigbergk, Jan 15, 2013
  52. 06/19 reset.c: remove unnecessary variable 'i'Martin von Zweigbergk, Jan 15, 2013
  53. 07/19 reset.c: extract function for updating {ORIG_,}HEADMartin von Zweigbergk, Jan 15, 2013
  54. 08/19 reset.c: share call to die_if_unmerged_cache()Martin von Zweigbergk, Jan 15, 2013
  55. 09/19 reset --keep: only write index file onceMartin von Zweigbergk, Jan 15, 2013
  56. 10/19 reset: avoid redundant error messageMartin von Zweigbergk, Jan 15, 2013
  57. 11/19 reset.c: replace switch by if-elseMartin von Zweigbergk, Jan 15, 2013
  58. 12/19 reset.c: move update_index_refresh() call out of read_from_tree()Martin von Zweigbergk, Jan 15, 2013
  59. 13/19 reset.c: move lock, write and commit out of update_index_refresh()Martin von Zweigbergk, Jan 15, 2013
  60. 14/19 reset [--mixed]: only write index file onceMartin von Zweigbergk, Jan 15, 2013
  61. 15/19 reset.c: finish entire cmd_reset() whether or not pathspec is givenMartin von Zweigbergk, Jan 15, 2013
  62. 16/19 reset.c: inline update_index_refresh()Martin von Zweigbergk, Jan 15, 2013
  63. 17/19 reset $sha1 $pathspec: require $sha1 only to be treeishMartin von Zweigbergk, Jan 15, 2013
  64. 17/19 fixup! reset $sha1 $pathspec: require $sha1 only to be treeishMartin von Zweigbergk, Jan 16, 2013
  65. Martin von ZweigbergkJan 16, 2013
  66. 18/19 reset: allow reset on unborn branchMartin von Zweigbergk, Jan 15, 2013
  67. 19/19 reset [--mixed]: use diff-based reset whether or not pathspec was givenMartin von Zweigbergk, Jan 15, 2013
  68. Martin von ZweigbergkJan 15, 2013

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.