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

[PATCH] commit: do not switch branch during a rebase unless -f is given

From
Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
Date
Sep 28, 2010, 04:52 UTC
Message-ID
<1285649564-24737-1-git-send-email-pclouds@gmail.com>

It does not make much sense to switch branch when you are in a middle of a rebase. Sometimes you might want to switch away for a moment then back with "git checkout - ". But I find myself so many times switching away then forget that I was rebasing something.

Avoid doing that by default. Users can use -f if they really want to.
Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com>
---
 I know there are other commands like rebase ("git am" comes to mind)
 but I don't use those. Feel free to put some more on top if somebody
 finds it a good thing to do.
 builtin/checkout.c         |    7 +++++++
 t/t2018-checkout-branch.sh |   22 ++++++++++++++++++++++
 2 files changed, 29 insertions(+), 0 deletions(-)
diff --git a/builtin/checkout.c b/builtin/checkout.c
index cc622c0..7d8ba04 100644
--- a/builtin/checkout.c
+++ b/builtin/checkout.c
@@ -571,6 +571,13 @@ static int switch_branches(struct checkout_opts *opts, struct branch_info *new)
 	struct branch_info old;
 	unsigned char rev[20];
 	int flag;
+	struct stat st;
+
+	if (!opts->force &&
+	    ((!stat(git_path("rebase-merge"), &st) && S_ISDIR(st.st_mode)) ||
+	     (!stat(git_path("rebase-apply"), &st) && S_ISDIR(st.st_mode))))
+		die("You should not switch branch during a rebase. Use '-f' if you really want to.");
+
 	memset(&old, 0, sizeof(old));
 	old.path = resolve_ref("HEAD", rev, 0, &flag);
 	old.commit = lookup_commit_reference_gently(rev, 1);
diff --git a/t/t2018-checkout-branch.sh b/t/t2018-checkout-branch.sh
index fa69016..95a1da8 100755
--- a/t/t2018-checkout-branch.sh
+++ b/t/t2018-checkout-branch.sh
@@ -169,4 +169,26 @@ test_expect_success 'checkout -f -B to an existing branch with mergeable changes
 	test_must_fail test_dirty_mergeable
 '
 
+test_expect_success 'checkout fails during rebase' '
+	git reset --hard &&
+	git checkout branch1 &&
+	mkdir .git/rebase-merge &&
+	test_must_fail git checkout branch2 &&
+	git checkout -f branch2
+'
+
+test_expect_success 'checkout fails during rebase (2)' '
+	rmdir .git/rebase-merge &&
+	git reset --hard &&
+	git checkout branch1 &&
+	mkdir .git/rebase-apply &&
+	test_must_fail git checkout branch2 &&
+	git checkout -f branch2
+'
+
+# this is to be incorporated to the next test
+test_expect_success 'cleanup' '
+	rmdir .git/rebase-apply
+'
+
 test_done
-- 
1.7.1.rc1.70.g788ca
Next: Nguyễn Thái Ngọc Duy
Message 1 of 12 in “commit: do not switch branch during a rebase unless -f is given”
  1. commit: do not switch branch during a rebase unless -f is givenNguyễn Thái Ngọc Duy, Sep 28, 2010
  2. checkout: do not switch branch during a rebase unless -f is givenNguyễn Thái Ngọc Duy, Sep 28, 2010
  3. Jonathan NiederSep 28, 2010
  4. Nguyen Thai Ngoc DuySep 28, 2010
  5. Nguyen Thai Ngoc DuyNov 9, 2010
  6. Jonathan NiederNov 9, 2010
  7. Sverre RabbelierNov 9, 2010
  8. Jonathan NiederNov 9, 2010
  9. Nguyen Thai Ngoc DuyNov 9, 2010
  10. Jonathan NiederNov 9, 2010
  11. Junio C HamanoSep 28, 2010
  12. Sverre RabbelierSep 28, 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.