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

[PATCH 3/3] builtin/checkout: DWIM with -f -b

From
Tay Ray Chuan <rctay89@gmail.com>
Date
Jun 20, 2010, 16:33 UTC
Message-ID
<1277051592-4552-4-git-send-email-rctay89@gmail.com>
In-Reply-To
<1277051592-4552-3-git-send-email-rctay89@gmail.com>
Signed-off-by: Tay Ray Chuan <rctay89@gmail.com>
---
 Documentation/git-checkout.txt |    3 +++
 builtin/checkout.c             |    4 ++--
 t/t2018-checkout-branch.sh     |    6 +++---
 3 files changed, 8 insertions(+), 5 deletions(-)
diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt
index 261dd90..d15d224 100644
--- a/Documentation/git-checkout.txt
+++ b/Documentation/git-checkout.txt
@@ -74,6 +74,9 @@ entries; instead, unmerged entries are ignored.
 -b::
 	Create a new branch named <new_branch> and start it at
 	<start_point>; see linkgit:git-branch[1] for details.
++
+When used with `-f`, the branch is reset; changes in the index or the
+working tree are thrown away too.
 
 -t::
 --track::
diff --git a/builtin/checkout.c b/builtin/checkout.c
index 1994be9..962d938 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -511,7 +511,7 @@ static void update_refs_for_switch(struct checkout_opts *opts,
 			}
 		}
 		else
-			create_branch(old->name, opts->new_branch, new->name, 0,
+			create_branch(old->name, opts->new_branch, new->name, opts->force,
 				      opts->new_branch_log, opts->track);
 		new->name = opts->new_branch;
 		setup_branch_path(new);
@@ -858,7 +858,7 @@ no_reference:
 		if (strbuf_check_branch_ref(&buf, opts.new_branch))
 			die("git checkout: we do not like '%s' as a branch name.",
 			    opts.new_branch);
-		if (!get_sha1(buf.buf, rev))
+		if (!get_sha1(buf.buf, rev) && !opts.force)
 			die("git checkout: branch %s already exists", opts.new_branch);
 		strbuf_release(&buf);
 	}
diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index e6c0f8c..9895ff5 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -63,19 +63,19 @@ test_expect_success 'checkout -b to an existing branch fails' '
 	test_must_fail do_checkout branch2 $HEAD2
 '
 
-test_expect_failure 'checkout -f -b to an existing branch resets branch' '
+test_expect_success 'checkout -f -b to an existing branch resets branch' '
 	git checkout branch1 &&
 
 	do_checkout branch2 "" -f
 '
 
-test_expect_failure 'checkout -f -b to an existing branch resets branch (explicit ref)' '
+test_expect_success 'checkout -f -b to an existing branch resets branch (explicit ref)' '
 	git checkout branch1 &&
 
 	do_checkout branch2 $HEAD1 -f
 '
 
-test_expect_failure 'checkout -f -b to an existing branch resets branch (dirty) ' '
+test_expect_success 'checkout -f -b to an existing branch resets branch (dirty) ' '
 	git checkout branch1 &&
 
 	setup_dirty &&
-- 
1.7.1.513.g4f18
Previous: Tay Ray ChuanNext: Junio C Hamano
Message 4 of 29 in “DWIM with "git checkout -f -b"”
  1. 0/3 DWIM with "git checkout -f -b"Tay Ray Chuan, Jun 20, 2010
  2. 1/3 add tests for checkout -bTay Ray Chuan, Jun 20, 2010
  3. 2/3 t2018-checkout-branch.sh: show that checkout -f -b doesn't DWIMTay Ray Chuan, Jun 20, 2010
  4. 3/3 builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 20, 2010
  5. Junio C HamanoJun 20, 2010
  6. Jeff KingJun 20, 2010
  7. Clemens BuchacherJun 20, 2010
  8. Junio C HamanoJun 20, 2010
  9. Jeff KingJun 20, 2010
  10. Junio C HamanoJun 21, 2010
  11. Tay Ray ChuanJun 21, 2010
  12. Junio C HamanoJun 21, 2010
  13. 0/3 teach git-checkout -B WAS builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 21, 2010
  14. 1/3 add tests for checkout -bTay Ray Chuan, Jun 21, 2010
  15. 2/3 builtin/checkout: change -b from an OPTION_STRING to a OPTION_SET_INTTay Ray Chuan, Jun 21, 2010
  16. 3/3 builtin/checkout: learn -BTay Ray Chuan, Jun 21, 2010
  17. Junio C HamanoJun 23, 2010
  18. Tay Ray ChuanJun 23, 2010
  19. Erick MattosJun 21, 2010
  20. Tay Ray ChuanJun 23, 2010
  21. Erick MattosJun 23, 2010
  22. Tay Ray ChuanJun 21, 2010
  23. Erick MattosJun 21, 2010
  24. 0/3 teach git-checkout -B WAS builtin/checkout: DWIM with -f -bTay Ray Chuan, Jun 23, 2010
  25. 1/3 add tests for checkout -bTay Ray Chuan, Jun 23, 2010
  26. 2/3 builtin/checkout: reword hint for -bTay Ray Chuan, Jun 23, 2010
  27. 3/3 builtin/checkout: learn -BTay Ray Chuan, Jun 23, 2010
  28. Michel LespinasseJun 21, 2010
  29. Michel LespinasseJun 21, 2010

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.