# [PATCH] history: refuse to rewrite branches used by other worktrees

7 messages from 2026-11-23 to 2026-11-24. Participants: Aniket Gupta, Phillip Wood, Junio C Hamano.
Thread: https://gitlist.dev/t/66921

## Aniket Gupta, 2026-11-23 10:39

Subject: [PATCH] history: refuse to rewrite branches used by other worktrees
Message-ID: <20261123103911.98860-1-aniketgupta3001@gmail.com>
URL: https://gitlist.dev/e/20261123103911.98860-1-aniketgupta3001%40gmail.com

```
By default "git history" also rewrites every local branch that descends
from the commit it changes. If another worktree has such a branch
checked out, the branch moves but the index and working tree of that
worktree stay behind. So it shows staged changes that nobody made. If
the current worktree is in the middle of rebasing the branch, "git
rebase --continue" later fails because the branch is not where the
rebase expects it.

"git branch -f" refuses to move such branches and "git rebase
--update-refs" skips them. Do the same here: before replaying anything
look for local branches that contain the commit and are used by a
worktree and refuse to go on if there is one. Use branch_checked_out()
so that a branch that is being rebased or bisected counts as used. Skip
the branch that the current worktree has checked out, since the command
already takes care of the index and working tree of that worktree.
Refuse with --dry-run as well because it will show what a real run will
do.

Mention --update-refs=head, which leaves the other branches alone, with
the existing advice.historyUpdateRefs hint.

Reported-by: Toon Claes <toon@iotcl.com>
Reported-by: Nikita Bobko <nikita@nbobko.com>
Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com>
---
Patrick suggested this in [1] and Toon said I could pick it up [2].
This is based on master as it changes behavior.

[1] https://lore.kernel.org/git/aob_LsfI9cO3PewF@pks.im/
[2] https://lore.kernel.org/git/8733ssj5zt.fsf@dev.null.iotcl.com.invalid/

 Documentation/config/advice.adoc |   7 +-
 Documentation/git-history.adoc   |   6 ++
 builtin/history.c                |  61 +++++++++++++++
 t/t3450-history.sh               | 128 +++++++++++++++++++++++++++++++
 4 files changed, 199 insertions(+), 3 deletions(-)

diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc
index 3d91c90eda..bb0e24b745 100644
--- a/Documentation/config/advice.adoc
+++ b/Documentation/config/advice.adoc
@@ -60,9 +60,10 @@ all advice messages.
 		Shown when the user tries to delete a not fully merged
 		branch without the force option set.
 	historyUpdateRefs::
-		Shown when `git history squash` refuses because a local branch
-		cannot be rewritten as a descendant of the squashed commit, to
-		tell the user about `--update-refs=head`.
+		Shown when `git history` refuses because a local branch is used
+		by another worktree, or, for `squash`, because it cannot be
+		rewritten as a descendant of the squashed commit, to tell the
+		user about `--update-refs=head`.
 	ignoredHook::
 		Shown when a hook is ignored because the hook is not
 		set as executable.
diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc
index 1fcc30150a..ce7c0a88c8 100644
--- a/Documentation/git-history.adoc
+++ b/Documentation/git-history.adoc
@@ -215,6 +215,12 @@ empty.
 	`branches`, all local branches that point to commits which are
 	descendants of the original commit will be rewritten. With `head`, only
 	the current `HEAD` reference will be rewritten. Defaults to `branches`.
++
+A branch that is used by another worktree, or that the current worktree is
+in the middle of rebasing or bisecting, cannot be rewritten, as the index and
+working tree of that worktree would not match the rewritten branch. With
+`branches` the command refuses in that case, so use `head` to rewrite only
+the current branch.
 
 EXAMPLES
 --------
diff --git a/builtin/history.c b/builtin/history.c
index 43ba76f5a1..90f00f676d 100644
--- a/builtin/history.c
+++ b/builtin/history.c
@@ -2,6 +2,7 @@
 
 #include "builtin.h"
 #include "advice.h"
+#include "branch.h"
 #include "cache-tree.h"
 #include "commit.h"
 #include "commit-reach.h"
@@ -295,6 +296,62 @@ static int revwalk_contains_merges(struct repository *repo,
 	return ret;
 }
 
+/*
+ * Rewriting a branch that is used by another worktree moves the branch
+ * without updating the index and working tree of that worktree. The same
+ * goes for the branch that the current worktree is rebasing. Refuse to
+ * rewrite such branches, but not the branch that the current worktree has
+ * checked out, as we update that one ourselves.
+ */
+static int reject_checked_out_branches(struct repository *repo,
+				       struct commit *original)
+{
+	struct ref_filter filter = REF_FILTER_INIT;
+	struct ref_array refs = { 0 };
+	char *head_target;
+	int ret = 0;
+
+	head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD",
+					  RESOLVE_REF_NO_RECURSE, NULL, NULL);
+
+	commit_list_insert(original, &filter.with_commit);
+	filter.kind = FILTER_REFS_BRANCHES;
+	if (filter_refs(&refs, &filter, filter.kind)) {
+		ret = error(_("could not filter refs"));
+		goto out;
+	}
+
+	for (int i = 0; i < refs.nr; i++) {
+		const char *refname = refs.items[i]->refname;
+		const char *branch, *path;
+
+		if (head_target && !strcmp(refname, head_target))
+			continue;
+
+		path = branch_checked_out(refname);
+		if (!path)
+			continue;
+
+		if (!skip_prefix(refname, "refs/heads/", &branch))
+			BUG("branch filter returned '%s'", refname);
+		error(_("cannot rewrite branch '%s' used by worktree at '%s'"),
+		      branch, path);
+		ret = -1;
+	}
+
+	if (ret)
+		advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS,
+				  _("Use --update-refs=head to rewrite only "
+				    "the current branch and leave such branches "
+				    "untouched."));
+
+out:
+	free(head_target);
+	ref_filter_clear(&filter);
+	ref_array_clear(&refs);
+	return ret;
+}
+
 static int setup_revwalk(struct repository *repo,
 			 enum ref_action action,
 			 struct commit *original,
@@ -350,6 +407,10 @@ static int setup_revwalk(struct repository *repo,
 
 		strvec_push(&args, "HEAD");
 	} else {
+		ret = reject_checked_out_branches(repo, original);
+		if (ret < 0)
+			goto out;
+
 		strvec_push(&args, "--branches");
 		strvec_push(&args, "HEAD");
 	}
diff --git a/t/t3450-history.sh b/t/t3450-history.sh
index f513463b92..f7ca66edc6 100755
--- a/t/t3450-history.sh
+++ b/t/t3450-history.sh
@@ -14,4 +14,132 @@ test_expect_success 'raises an error with unknown argument' '
 	test_grep "unknown subcommand: .garbage." err
 '
 
+test_expect_success 'refuses to rewrite a branch checked out in another worktree' '
+	test_when_finished "rm -rf repo wt" &&
+	git init repo --initial-branch=main &&
+	(
+		cd repo &&
+		test_commit first &&
+		test_commit second &&
+		test_commit third &&
+		git worktree add ../wt -b other &&
+
+		git refs list >refs-expect &&
+		test_must_fail git history drop second 2>err &&
+		test_grep "^error: cannot rewrite branch .other. used by worktree at .*wt" err &&
+		test_grep "^hint: .* --update-refs=head" err &&
+		test_grep ! "branch .main." err &&
+		git refs list >refs-actual &&
+		test_cmp refs-expect refs-actual
+	)
+'
+
+test_expect_success 'refusing to rewrite checked out branches honors --dry-run' '
+	test_when_finished "rm -rf repo wt" &&
+	git init repo --initial-branch=main &&
+	(
+		cd repo &&
+		test_commit first &&
+		test_commit second &&
+		test_commit third &&
+		git worktree add ../wt -b other &&
+
+		test_must_fail git history drop --dry-run second >out 2>err &&
+		test_grep "^error: cannot rewrite branch .other. used by worktree at" err &&
+		test_must_be_empty out
+	)
+'
+
+test_expect_success 'advice.historyUpdateRefs silences the hint for checked out branches' '
+	test_when_finished "rm -rf repo wt" &&
+	git init repo --initial-branch=main &&
+	(
+		cd repo &&
+		test_commit first &&
+		test_commit second &&
+		test_commit third &&
+		git worktree add ../wt -b other &&
+
+		test_must_fail git -c advice.historyUpdateRefs=false \
+			history drop second 2>err &&
+		test_grep "^error: cannot rewrite branch .other. used by worktree at" err &&
+		test_grep ! "hint:" err
+	)
+'
+
+test_expect_success '--update-refs=head leaves branches of other worktrees alone' '
+	test_when_finished "rm -rf repo wt" &&
+	git init repo --initial-branch=main &&
+	(
+		cd repo &&
+		test_commit first &&
+		test_commit second &&
+		test_commit third &&
+		git worktree add ../wt -b other &&
+		git rev-parse other >other-expect &&
+
+		git history drop --update-refs=head second &&
+
+		git rev-parse other >other-actual &&
+		test_cmp other-expect other-actual &&
+		git log --format=%s main >actual &&
+		test_write_lines third first >expect &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'rewrites the branch of the current worktree' '
+	test_when_finished "rm -rf repo wt" &&
+	git init repo --initial-branch=main &&
+	(
+		cd repo &&
+		test_commit first &&
+		git worktree add ../wt -b other &&
+		test_commit -C ../wt second &&
+		test_commit -C ../wt third &&
+
+		git -C ../wt history drop second &&
+
+		git log --format=%s other >actual &&
+		test_write_lines third first >expect &&
+		test_cmp expect actual
+	)
+'
+
+test_expect_success 'refuses to rewrite a branch that is being rebased' '
+	test_when_finished "rm -rf repo" &&
+	git init repo --initial-branch=main &&
+	(
+		cd repo &&
+		test_commit first &&
+		test_commit second &&
+		test_commit third &&
+		test_must_fail git rebase -x false first &&
+
+		git refs list >refs-expect &&
+		test_must_fail git history drop second 2>err &&
+		test_grep "^error: cannot rewrite branch .main. used by worktree at" err &&
+		git refs list >refs-actual &&
+		test_cmp refs-expect refs-actual &&
+
+		git rebase --abort
+	)
+'
+
+test_expect_success 'refuses to rewrite a branch that is being bisected' '
+	test_when_finished "rm -rf repo wt" &&
+	git init repo --initial-branch=main &&
+	(
+		cd repo &&
+		test_commit first &&
+		test_commit second &&
+		test_commit third &&
+		git worktree add ../wt -b other &&
+		git -C ../wt bisect start HEAD first &&
+
+		test_must_fail git history drop second 2>err &&
+		test_grep "^error: cannot rewrite branch .other. used by worktree at" err
+	)
+'
+
 test_done

base-commit: f7de81456940dd6f3274e461149bba12965de609
-- 
2.54.0 (Apple Git-157)



```

## Phillip Wood, 2026-11-23 13:10

Subject: Re: [PATCH] history: refuse to rewrite branches used by other worktrees
Message-ID: <a0ebd98f-669e-4ff2-bca2-cfd5da5a7316@gmail.com>
URL: https://gitlist.dev/e/a0ebd98f-669e-4ff2-bca2-cfd5da5a7316%40gmail.com
In-Reply-To: <20261123103911.98860-1-aniketgupta3001@gmail.com>

```
Hi Aniket

Thanks for working on this, it is a useful improvement.

On 23/11/2026 10:39, Aniket Gupta wrote:
> By default "git history" also rewrites every local branch that descends

"also" is unnecessary here

> from the commit it changes. If another worktree has such a branch
> checked out, the branch moves but the index and working tree of that
> worktree stay behind. So it shows staged changes that nobody made.

The staged changes it shows were made by "git history" - they did not appear from nowhere. An alternative to refusing to run would be to update those worktrees if they are clean, but failing is the simpler option to implement.

> If
> the current worktree is in the middle of rebasing the branch, "git
> rebase --continue" later fails because the branch is not where the
> rebase expects it.
> > "git branch -f" refuses to move such branches and "git rebase
> --update-refs" skips them. Do the same here: before replaying anything
> look for local branches that contain the commit and are used by a
> worktree and refuse to go on if there is one. Use branch_checked_out()
> so that a branch that is being rebased or bisected counts as used.

Good. One thing to note is that I was recently looking at branch_checked_out() and it only stores a single worktree per ref, so I think if there are multiple checkouts of the same branch and we happen to record the current worktree as the one where it is checked out we'll fail to notice it is checked out elsewhere. That's not a problem that needs solving here though.

> Skip
> the branch that the current worktree has checked out, since the command
> already takes care of the index and working tree of that worktree.

What happens if a rebase is running in the same worktree as the user tries to run "git history"?

> Refuse with --dry-run as well because it will show what a real run will
> do.

Good

> Mention --update-refs=head, which leaves the other branches alone, with
> the existing advice.historyUpdateRefs hint.

Does --update-refs=head try to update the current branch? If so we should check that is not being written somewhere else.

> > diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc
> index 3d91c90eda..bb0e24b745 100644
> --- a/Documentation/config/advice.adoc
> +++ b/Documentation/config/advice.adoc
> @@ -60,9 +60,10 @@ all advice messages.
>   		Shown when the user tries to delete a not fully merged
>   		branch without the force option set.
>   	historyUpdateRefs::
> -		Shown when `git history squash` refuses because a local branch
> -		cannot be rewritten as a descendant of the squashed commit, to
> -		tell the user about `--update-refs=head`.
> +		Shown when `git history` refuses because a local branch is used

This is copied from the existing wording, so it is not a new problem but we should say what "git history" refuses to do. e.g.

	Shown when 'git history' refuses to run because ...

> +		by another worktree, or, for `squash`, because it cannot be
> +		rewritten as a descendant of the squashed commit, to tell the
> +		user about `--update-refs=head`.
>   	ignoredHook::
>   		Shown when a hook is ignored because the hook is not
>   		set as executable.
> diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc
> index 1fcc30150a..ce7c0a88c8 100644
> --- a/Documentation/git-history.adoc
> +++ b/Documentation/git-history.adoc
> @@ -215,6 +215,12 @@ empty.
>   	`branches`, all local branches that point to commits which are
>   	descendants of the original commit will be rewritten. With `head`, only
>   	the current `HEAD` reference will be rewritten. Defaults to `branches`.
> ++
> +A branch that is used by another worktree, or that the current worktree is
> +in the middle of rebasing or bisecting, cannot be rewritten, as the index and
> +working tree of that worktree would not match the rewritten branch. With> +`branches` the command refuses in that case, so use `head` to rewrite only
> +the current branch.

We should also prevent rebase from rewriting a branch that is being rewritten by "git history" but that can come later.

> +/*
> + * Rewriting a branch that is used by another worktree moves the branch
> + * without updating the index and working tree of that worktree. The same
> + * goes for the branch that the current worktree is rebasing. Refuse to
> + * rewrite such branches, but not the branch that the current worktree has
> + * checked out, as we update that one ourselves.
> + */
> +static int reject_checked_out_branches(struct repository *repo,
> +				       struct commit *original)
> +{
> +	struct ref_filter filter = REF_FILTER_INIT;
> +	struct ref_array refs = { 0 };
> +	char *head_target;
> +	int ret = 0;
> +
> +	head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD",
> +					  RESOLVE_REF_NO_RECURSE, NULL, NULL);
> +
> +	commit_list_insert(original, &filter.with_commit);
> +	filter.kind = FILTER_REFS_BRANCHES;
> +	if (filter_refs(&refs, &filter, filter.kind)) {

This is correct, but it duplicates the revision walk we're already doing when looking for merge commits. It would be more efficient to

(1) modify setup_revwalk() to load decorations (we already load them
    when replaying the commits so there is no extra work, we're just
    loading them earlier)
(2) modify and rename revwalk_contains_merges() to
  (a) drop"--min-parents" and check for merges by looking at
      commit->parents->next.
  (b) check DECORATION_REF_LOCAL decorations are not checked out
      elsewhere.

For an existing example of using decorations, look at replay.c:replay_revisions().

Thanks

Phillip

> +		ret = error(_("could not filter refs"));
> +		goto out;
> +	}
> +
> +	for (int i = 0; i < refs.nr; i++) {
> +		const char *refname = refs.items[i]->refname;
> +		const char *branch, *path;
> +
> +		if (head_target && !strcmp(refname, head_target))
> +			continue;
> +
> +		path = branch_checked_out(refname);
> +		if (!path)
> +			continue;
> +
> +		if (!skip_prefix(refname, "refs/heads/", &branch))
> +			BUG("branch filter returned '%s'", refname);
> +		error(_("cannot rewrite branch '%s' used by worktree at '%s'"),
> +		      branch, path);
> +		ret = -1;
> +	}
> +
> +	if (ret)
> +		advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS,
> +				  _("Use --update-refs=head to rewrite only "
> +				    "the current branch and leave such branches "
> +				    "untouched."));
> +
> +out:
> +	free(head_target);
> +	ref_filter_clear(&filter);
> +	ref_array_clear(&refs);
> +	return ret;
> +}
> +
>   static int setup_revwalk(struct repository *repo,
>   			 enum ref_action action,
>   			 struct commit *original,
> @@ -350,6 +407,10 @@ static int setup_revwalk(struct repository *repo,
>   >   		strvec_push(&args, "HEAD");
>   	} else {
> +		ret = reject_checked_out_branches(repo, original);
> +		if (ret < 0)
> +			goto out;
> +
>   		strvec_push(&args, "--branches");
>   		strvec_push(&args, "HEAD");
>   	}
> diff --git a/t/t3450-history.sh b/t/t3450-history.sh
> index f513463b92..f7ca66edc6 100755
> --- a/t/t3450-history.sh
> +++ b/t/t3450-history.sh
> @@ -14,4 +14,132 @@ test_expect_success 'raises an error with unknown argument' '
>   	test_grep "unknown subcommand: .garbage." err
>   '
>   > +test_expect_success 'refuses to rewrite a branch checked out in another worktree' '
> +	test_when_finished "rm -rf repo wt" &&
> +	git init repo --initial-branch=main &&
> +	(
> +		cd repo &&
> +		test_commit first &&
> +		test_commit second &&
> +		test_commit third &&
> +		git worktree add ../wt -b other &&
> +
> +		git refs list >refs-expect &&
> +		test_must_fail git history drop second 2>err &&
> +		test_grep "^error: cannot rewrite branch .other. used by worktree at .*wt" err &&
> +		test_grep "^hint: .* --update-refs=head" err &&
> +		test_grep ! "branch .main." err &&
> +		git refs list >refs-actual &&
> +		test_cmp refs-expect refs-actual
> +	)
> +'
> +
> +test_expect_success 'refusing to rewrite checked out branches honors --dry-run' '
> +	test_when_finished "rm -rf repo wt" &&
> +	git init repo --initial-branch=main &&
> +	(
> +		cd repo &&
> +		test_commit first &&
> +		test_commit second &&
> +		test_commit third &&
> +		git worktree add ../wt -b other &&
> +
> +		test_must_fail git history drop --dry-run second >out 2>err &&
> +		test_grep "^error: cannot rewrite branch .other. used by worktree at" err &&
> +		test_must_be_empty out
> +	)
> +'
> +
> +test_expect_success 'advice.historyUpdateRefs silences the hint for checked out branches' '
> +	test_when_finished "rm -rf repo wt" &&
> +	git init repo --initial-branch=main &&
> +	(
> +		cd repo &&
> +		test_commit first &&
> +		test_commit second &&
> +		test_commit third &&
> +		git worktree add ../wt -b other &&
> +
> +		test_must_fail git -c advice.historyUpdateRefs=false \
> +			history drop second 2>err &&
> +		test_grep "^error: cannot rewrite branch .other. used by worktree at" err &&
> +		test_grep ! "hint:" err
> +	)
> +'
> +
> +test_expect_success '--update-refs=head leaves branches of other worktrees alone' '
> +	test_when_finished "rm -rf repo wt" &&
> +	git init repo --initial-branch=main &&
> +	(
> +		cd repo &&
> +		test_commit first &&
> +		test_commit second &&
> +		test_commit third &&
> +		git worktree add ../wt -b other &&
> +		git rev-parse other >other-expect &&
> +
> +		git history drop --update-refs=head second &&
> +
> +		git rev-parse other >other-actual &&
> +		test_cmp other-expect other-actual &&
> +		git log --format=%s main >actual &&
> +		test_write_lines third first >expect &&
> +		test_cmp expect actual
> +	)
> +'
> +
> +test_expect_success 'rewrites the branch of the current worktree' '
> +	test_when_finished "rm -rf repo wt" &&
> +	git init repo --initial-branch=main &&
> +	(
> +		cd repo &&
> +		test_commit first &&
> +		git worktree add ../wt -b other &&
> +		test_commit -C ../wt second &&
> +		test_commit -C ../wt third &&
> +
> +		git -C ../wt history drop second &&
> +
> +		git log --format=%s other >actual &&
> +		test_write_lines third first >expect &&
> +		test_cmp expect actual
> +	)
> +'
> +
> +test_expect_success 'refuses to rewrite a branch that is being rebased' '
> +	test_when_finished "rm -rf repo" &&
> +	git init repo --initial-branch=main &&
> +	(
> +		cd repo &&
> +		test_commit first &&
> +		test_commit second &&
> +		test_commit third &&
> +		test_must_fail git rebase -x false first &&
> +
> +		git refs list >refs-expect &&
> +		test_must_fail git history drop second 2>err &&
> +		test_grep "^error: cannot rewrite branch .main. used by worktree at" err &&
> +		git refs list >refs-actual &&
> +		test_cmp refs-expect refs-actual &&
> +
> +		git rebase --abort
> +	)
> +'
> +
> +test_expect_success 'refuses to rewrite a branch that is being bisected' '
> +	test_when_finished "rm -rf repo wt" &&
> +	git init repo --initial-branch=main &&
> +	(
> +		cd repo &&
> +		test_commit first &&
> +		test_commit second &&
> +		test_commit third &&
> +		git worktree add ../wt -b other &&
> +		git -C ../wt bisect start HEAD first &&
> +
> +		test_must_fail git history drop second 2>err &&
> +		test_grep "^error: cannot rewrite branch .other. used by worktree at" err
> +	)
> +'
> +
>   test_done
> > base-commit: f7de81456940dd6f3274e461149bba12965de609



```

## Junio C Hamano, 2026-11-23 20:14

Subject: Re: [PATCH] history: refuse to rewrite branches used by other worktrees
Message-ID: <xmqqa4mz5mr8.fsf@gitster.g>
URL: https://gitlist.dev/e/xmqqa4mz5mr8.fsf%40gitster.g
In-Reply-To: <20261123103911.98860-1-aniketgupta3001@gmail.com>

```
Aniket Gupta <aniketgupta3001@gmail.com> writes:

> +/*
> + * Rewriting a branch that is used by another worktree moves the branch
> + * without updating the index and working tree of that worktree. The same
> + * goes for the branch that the current worktree is rebasing. Refuse to
> + * rewrite such branches, but not the branch that the current worktree has
> + * checked out, as we update that one ourselves.
> + */

The intent is good.

> +static int reject_checked_out_branches(struct repository *repo,
> +				       struct commit *original)
> +{
> +	struct ref_filter filter = REF_FILTER_INIT;
> +	struct ref_array refs = { 0 };
> +	char *head_target;
> +	int ret = 0;
> +
> +	head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD",
> +					  RESOLVE_REF_NO_RECURSE, NULL, NULL);
> +
> +	commit_list_insert(original, &filter.with_commit);
> +	filter.kind = FILTER_REFS_BRANCHES;
> +	if (filter_refs(&refs, &filter, filter.kind)) {
> +		ret = error(_("could not filter refs"));
> +		goto out;
> +	}
> +
> +	for (int i = 0; i < refs.nr; i++) {
> +		const char *refname = refs.items[i]->refname;
> +		const char *branch, *path;
> +
> +		if (head_target && !strcmp(refname, head_target))
> +			continue;

Imagine we have a bare repository repo.git whose primary branch is
'main'.  Its HEAD records refs/heads/main (which is a common state
after you run "git clone --bare" from a modern project whose primary
branch is 'main').  Then you can add a worktree ../wt to check out the
'main' branch, because branch_checked_out() will say "no" when you
create and check out 'main' in that new worktree.

In that setup, what would running "git history" in the bare repository
to, say, drop a commit, do?  The 'main' branch is checked out
elsewhere in the worktree at ../wt; we run "git history" in the bare
repository, and head_target above is resolved to "refs/heads/main".
This 'continue' triggers for the 'main' branch, so we return ret == 0.

> +		path = branch_checked_out(refname);
> +		if (!path)
> +			continue;
> +
> +		if (!skip_prefix(refname, "refs/heads/", &branch))
> +			BUG("branch filter returned '%s'", refname);
> +		error(_("cannot rewrite branch '%s' used by worktree at '%s'"),
> +		      branch, path);
> +		ret = -1;
> +	}
> +
> +	if (ret)
> +		advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS,
> +				  _("Use --update-refs=head to rewrite only "
> +				    "the current branch and leave such branches "
> +				    "untouched."));
> +
> +out:
> +	free(head_target);
> +	ref_filter_clear(&filter);
> +	ref_array_clear(&refs);
> +	return ret;
> +}

Now, the following is all outside the context of this patch, but
looking at cmd_history_drop(), it calls setup_revwalk() which in turn
calls the above function, which does not complain in this case.

And is_bare_repository() is true.  We skip update_worktree() and
update the 'main' branch via its HEAD, making the index and the
worktree of the worktree at ../wt out of sync, no?

Perhaps head_target should be initialized to NULL and assigned the
resolution result of HEAD only in a non-bare repository, or something?
I didn't trace all other callers, so there may be other glitches I did
not notice.

The proposed log message claims to "Skip the branch that the current
worktree has checked out, since the command already takes care of the
index and working tree of that worktree", but as we saw above, it is
inaccurate for bare repositories.  It probably is also worth noting in
the log message that 'drop' updates the worktree while 'reword',
'split', and 'squash' preserve the tip tree and 'fixup' incorporates
staged changes from the index.

Thanks, I think this patch still needs some work.



```

## Aniket Gupta, 2026-11-23 21:54

Subject: Re: [PATCH] history: refuse to rewrite branches used by other worktrees
Message-ID: <20261123215424.27553-1-aniketgupta3001@gmail.com>
URL: https://gitlist.dev/e/20261123215424.27553-1-aniketgupta3001%40gmail.com
In-Reply-To: <xmqqa4mz5mr8.fsf@gitster.g>

```
Thanks for taking a look!

> And is_bare_repository() is true.  We skip update_worktree() and
> update the 'main' branch via its HEAD, making the index and the
> worktree of the worktree at ../wt out of sync, no?

True. I am able to repro this. In a bare repo nobody has the branch that
HEAD points to checked out, but the check skips it as if the current
worktree did. So "git history drop" moves "main" and ../wt shows the
changes of the dropped commit as staged.

> Perhaps head_target should be initialized to NULL and assigned the
> resolution result of HEAD only in a non-bare repository, or something?

I think that would fix it. In v2 I'd like to go a bit further though:
ask the other worktrees if they use the current branch instead of
skipping it by name. That also covers a bare repo and a branch that two
worktrees have checked out with "git worktree add --force". Will add
tests for both.

> I didn't trace all other callers, so there may be other glitches I
> did not notice.

Went through them. fixup, reword, split, squash, drop all call
setup_revwalk() before they open an editor, create a commit or update a
ref so they refuse early. Will add a test that runs each of them.

> It probably is also worth noting in
> the log message that 'drop' updates the worktree while 'reword',
> 'split', and 'squash' preserve the tip tree and 'fixup' incorporates
> staged changes from the index.

Point taken. Will fix the log message and add that.


```

## Aniket Gupta, 2026-11-23 21:54

Subject: Re: [PATCH] history: refuse to rewrite branches used by other worktrees
Message-ID: <20261123215431.27574-1-aniketgupta3001@gmail.com>
URL: https://gitlist.dev/e/20261123215431.27574-1-aniketgupta3001%40gmail.com
In-Reply-To: <a0ebd98f-669e-4ff2-bca2-cfd5da5a7316@gmail.com>

```
Thanks for taking a look!

> "also" is unnecessary here

Will drop this.

> The staged changes it shows were made by "git history" - they did not
> appear from nowhere. An alternative to refusing to run would be to
> update those worktrees if they are clean, but failing is the simpler
> option to implement.

Fair point, will reword. I stayed with refusing as it is simpler.
Updating clean worktrees could come later if people want it.

> fail to notice it is checked out elsewhere. That's not a problem that
> needs solving here though.

Noted. For the current branch v2 asks each worktree, so it does not
depend on branch_checked_out() there. The other branches still go
through it.

> What happens if a rebase is running in the same worktree as the user
> tries to run "git history"?

It is refused. HEAD is detached then, so the branch being rebased is not
the one this worktree has checked out, and it gets treated like any
other branch in use. There is a test for it, but the commit message does
not contain this. Will fix.

In that case --update-refs=head is not refused: it only rewrites the
detached HEAD, same as before. I tried fixup, reword, drop and squash
that way, and "git rebase --continue" finished fine each time.

> Does --update-refs=head try to update the current branch? If so we
> should check that is not being written somewhere else.

Yes, it updates the branch HEAD points to and nothing checks it today.
I could repro it with "git worktree add --force" on the same
branch: after a fixup in one of them, the other shows a staged change.
Good catch, will add the check.

> we should say what "git history" refuses to do.

Will fix.

> We should also prevent rebase from rewriting a branch that is being
> rewritten by "git history" but that can come later.

Agreed, that can come later.

> This is correct, but it duplicates the revision walk we're already
> doing when looking for merge commits.

Makes sense. I gave it a try and it works. One point: the branch
pointing at the rewritten commit itself is not part of the walk, so I
look at its decorations too. Rewriting a commit that another worktree's
branch points at goes through without it. Will do it like this in v2.


```

## Phillip Wood, 2026-11-24 10:49

Subject: Re: [PATCH] history: refuse to rewrite branches used by other worktrees
Message-ID: <14c971ef-438c-4197-b0cd-df9bf0ef0ae4@gmail.com>
URL: https://gitlist.dev/e/14c971ef-438c-4197-b0cd-df9bf0ef0ae4%40gmail.com
In-Reply-To: <20261123215431.27574-1-aniketgupta3001@gmail.com>

```
On 23/11/2026 21:54, Aniket Gupta wrote:
> Thanks for taking a look!

You're welcome, thanks for working on it.

>> What happens if a rebase is running in the same worktree as the user
>> tries to run "git history"?
> > It is refused. HEAD is detached then, so the branch being rebased is not
> the one this worktree has checked out, and it gets treated like any
> other branch in use. There is a test for it, but the commit message does
> not contain this. Will fix.
> > In that case --update-refs=head is not refused: it only rewrites the
> detached HEAD, same as before. I tried fixup, reword, drop and squash
> that way, and "git rebase --continue" finished fine each time.

That sounds like a bug though, because "git history" will update HEAD in the middle of a rebase and we throw away the result of running "git history" when "git rebase" completes.

> >> We should also prevent rebase from rewriting a branch that is being
>> rewritten by "git history" but that can come later.
> > Agreed, that can come later.

we should also refuse to run if another "git history" is rewriting the same branches - that's also something for another time.

>> This is correct, but it duplicates the revision walk we're already
>> doing when looking for merge commits.
> > Makes sense. I gave it a try and it works. One point: the branch
> pointing at the rewritten commit itself is not part of the walk, so I
> look at its decorations too.

Great, I realized that after I sent my mail yesterday, well spotted.

Thanks

Phillip

> Rewriting a commit that another worktree's
> branch points at goes through without it. Will do it like this in v2.



```

## Aniket Gupta, 2026-11-24 14:09

Subject: Re: [PATCH] history: refuse to rewrite branches used by other worktrees
Message-ID: <20261124140905.47213-1-aniketgupta3001@gmail.com>
URL: https://gitlist.dev/e/20261124140905.47213-1-aniketgupta3001%40gmail.com
In-Reply-To: <14c971ef-438c-4197-b0cd-df9bf0ef0ae4@gmail.com>

```
> That sounds like a bug though, because "git history" will update
> HEAD in the middle of a rebase and we throw away the result of
> running "git history" when "git rebase" completes.

It was wrong to say it is refused. That only happens when the commit
is still on the branch being rebased. My bad. Otherwise, the rewrite
is gone after "git rebase --abort". Will refuse in both modes.


```
