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

[PATCH 2/3] rebase: support --ignore-other-worktrees

From
GSGabriel Scherer <gabriel.scherer@inria.fr>
Date
Sep 13, 2025, 14:13 UTC
Message-ID
<20250913141327.2775228-3-gabriel.scherer@inria.fr>
In-Reply-To
<20250913141327.2775228-1-gabriel.scherer@inria.fr>
From: "Gabriel.Scherer" <gabriel.scherer@inria.fr>

rebase can currently fail if the branch to rebase is checked out in another worktree, and there is no way for users to override this error. We add support for the '--ignore-other-worktrees' option of 'checkout'.

Signed-off-by: Gabriel Scherer <gabriel.scherer@inria.fr>
---
 Documentation/git-rebase.adoc |  6 ++++++
 builtin/rebase.c              | 11 ++++++++++-
 t/t3400-rebase.sh             |  4 +++-
 3 files changed, 19 insertions(+), 2 deletions(-)
diff --git a/Documentation/git-rebase.adoc b/Documentation/git-rebase.adoc
index 005caf6164..b703d4056e 100644
--- a/Documentation/git-rebase.adoc
+++ b/Documentation/git-rebase.adoc
@@ -305,6 +305,12 @@ see the `--empty` flag.
 +
 See also INCOMPATIBLE OPTIONS below.
 
+--ignore-other-worktrees::
+	By default, `git rebase` refuses when the branch to rebase is
+	already checked out or otherwise in use by another
+	worktree. With this option, other worktrees are ignored and
+	the rebase proceeds anyway.
+
 --reapply-cherry-picks::
 --no-reapply-cherry-picks::
 	Reapply all clean cherry-picks of any upstream commit instead
diff --git a/builtin/rebase.c b/builtin/rebase.c
index 3c85768d29..7a57ebd852 100644
--- a/builtin/rebase.c
+++ b/builtin/rebase.c
@@ -128,6 +128,7 @@ struct rebase_options {
 	struct strbuf git_format_patch_opt;
 	int reschedule_failed_exec;
 	int reapply_cherry_picks;
+	int ignore_other_worktrees;
 	int fork_point;
 	int update_refs;
 	int config_autosquash;
@@ -146,6 +147,7 @@ struct rebase_options {
 		.git_format_patch_opt = STRBUF_INIT,	\
 		.fork_point = -1,			\
 		.reapply_cherry_picks = -1,             \
+		.ignore_other_worktrees = -1,           \
 		.allow_empty_message = 1,               \
 		.autosquash = -1,                       \
 		.rebase_merges = -1,                    \
@@ -1234,6 +1236,8 @@ int cmd_rebase(int argc,
 			 N_("automatically re-schedule any `exec` that fails")),
 		OPT_BOOL(0, "reapply-cherry-picks", &options.reapply_cherry_picks,
 			 N_("apply all changes, even those already present upstream")),
+		OPT_BOOL(0, "ignore-other-worktrees", &options.ignore_other_worktrees,
+			 N_("do not check if another worktree is using the branch to rebase")),
 		OPT_END(),
 	};
 	int i;
@@ -1580,6 +1584,10 @@ int cmd_rebase(int argc,
 			(options.flags & REBASE_INTERACTIVE_EXPLICIT);
 	}
 
+	if (options.ignore_other_worktrees == -1) {
+		options.ignore_other_worktrees = 0;
+	}
+
 	if (options.type == REBASE_UNSPECIFIED) {
 		if (!strcmp(options.default_backend, "merge"))
 			options.type = REBASE_MERGE;
@@ -1679,7 +1687,8 @@ int cmd_rebase(int argc,
 		strbuf_reset(&buf);
 		strbuf_addf(&buf, "refs/heads/%s", branch_name);
 		if (!refs_read_ref(get_main_ref_store(the_repository), buf.buf, &branch_oid)) {
-			die_if_checked_out(buf.buf, 1);
+			if (!options.ignore_other_worktrees)
+				die_if_checked_out(buf.buf, 1);
 			options.head_name = xstrdup(buf.buf);
 			options.orig_head =
 				lookup_commit_object(the_repository,
diff --git a/t/t3400-rebase.sh b/t/t3400-rebase.sh
index c0c00fbb7b..08448b4d4e 100755
--- a/t/t3400-rebase.sh
+++ b/t/t3400-rebase.sh
@@ -407,7 +407,9 @@ test_expect_success 'switch to branch checked out elsewhere fails' '
 	# we test in both worktrees to ensure that works
 	# as expected with "first" and "next" worktrees
 	test_must_fail git -C wt1 rebase shared shared &&
-	test_must_fail git -C wt2 rebase shared shared
+	test_must_fail git -C wt2 rebase shared shared &&
+        # with --ignore-other-worktrees the rebase succeeds
+	git -C wt1 rebase --ignore-other-worktrees shared shared
 '
 
 test_expect_success 'switch to branch not checked out' '
-- 
2.51.0
Previous: Kristoffer HaugsbakkNext: Phillip Wood
Message 14 of 20 in “extend --ignore-other-worktrees to 'rebase', add hints”
  1. 0/3 extend --ignore-other-worktrees to 'rebase', add hintsGabriel Scherer, Sep 13, 2025
  2. 1/3 checkout: provide hint when failing due to another worktreeGabriel Scherer, Sep 13, 2025
  3. Kristoffer HaugsbakkSep 13, 2025
  4. Gabriel SchererSep 14, 2025
  5. Junio C HamanoSep 15, 2025
  6. Gabriel SchererSep 15, 2025
  7. Junio C HamanoSep 16, 2025
  8. Junio C HamanoSep 17, 2025
  9. Gabriel SchererSep 17, 2025
  10. Phillip WoodSep 19, 2025
  11. Ben KnobleSep 14, 2025
  12. Ben KnobleSep 14, 2025
  13. Kristoffer HaugsbakkSep 14, 2025
  14. 2/3 rebase: support --ignore-other-worktreesGabriel Scherer, Sep 13, 2025
  15. Phillip WoodSep 15, 2025
  16. Junio C HamanoSep 15, 2025
  17. 3/3 rebase: hint when failing on branch used by another worktreeGabriel Scherer, Sep 13, 2025
  18. Ben KnobleSep 13, 2025
  19. Gabriel SchererSep 13, 2025
  20. Phillip WoodSep 15, 2025

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.