{"thread":{"id":"66921","subject":"[PATCH] history: refuse to rewrite branches used by other worktrees","startedAt":"2026-11-23T10:39:11Z","lastAt":"2026-11-24T14:09:05Z","messageCount":7,"participants":["Aniket Gupta","Phillip Wood","Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"559137","messageId":"20261123103911.98860-1-aniketgupta3001@gmail.com","threadId":"66921","inReplyTo":null,"subject":"[PATCH] history: refuse to rewrite branches used by other worktrees","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-23T10:39:11Z","receivedAt":"2026-11-23T10:39:11Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"By default \"git history\" also rewrites every local branch that descends\nfrom the commit it changes. If another worktree has such a branch\nchecked out, the branch moves but the index and working tree of that\nworktree stay behind. So it shows staged changes that nobody made. If\nthe current worktree is in the middle of rebasing the branch, \"git\nrebase --continue\" later fails because the branch is not where the\nrebase expects it.\n\n\"git branch -f\" refuses to move such branches and \"git rebase\n--update-refs\" skips them. Do the same here: before replaying anything\nlook for local branches that contain the commit and are used by a\nworktree and refuse to go on if there is one. Use branch_checked_out()\nso that a branch that is being rebased or bisected counts as used. Skip\nthe branch that the current worktree has checked out, since the command\nalready takes care of the index and working tree of that worktree.\nRefuse with --dry-run as well because it will show what a real run will\ndo.\n\nMention --update-refs=head, which leaves the other branches alone, with\nthe existing advice.historyUpdateRefs hint.\n\nReported-by: Toon Claes <toon@iotcl.com>\nReported-by: Nikita Bobko <nikita@nbobko.com>\nSigned-off-by: Aniket Gupta <aniketgupta3001@gmail.com>\n---\nPatrick suggested this in [1] and Toon said I could pick it up [2].\nThis is based on master as it changes behavior.\n\n[1] https://lore.kernel.org/git/aob_LsfI9cO3PewF@pks.im/\n[2] https://lore.kernel.org/git/8733ssj5zt.fsf@dev.null.iotcl.com.invalid/\n\n Documentation/config/advice.adoc |   7 +-\n Documentation/git-history.adoc   |   6 ++\n builtin/history.c                |  61 +++++++++++++++\n t/t3450-history.sh               | 128 +++++++++++++++++++++++++++++++\n 4 files changed, 199 insertions(+), 3 deletions(-)\n\ndiff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc\nindex 3d91c90eda..bb0e24b745 100644\n--- a/Documentation/config/advice.adoc\n+++ b/Documentation/config/advice.adoc\n@@ -60,9 +60,10 @@ all advice messages.\n \t\tShown when the user tries to delete a not fully merged\n \t\tbranch without the force option set.\n \thistoryUpdateRefs::\n-\t\tShown when `git history squash` refuses because a local branch\n-\t\tcannot be rewritten as a descendant of the squashed commit, to\n-\t\ttell the user about `--update-refs=head`.\n+\t\tShown when `git history` refuses because a local branch is used\n+\t\tby another worktree, or, for `squash`, because it cannot be\n+\t\trewritten as a descendant of the squashed commit, to tell the\n+\t\tuser about `--update-refs=head`.\n \tignoredHook::\n \t\tShown when a hook is ignored because the hook is not\n \t\tset as executable.\ndiff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\nindex 1fcc30150a..ce7c0a88c8 100644\n--- a/Documentation/git-history.adoc\n+++ b/Documentation/git-history.adoc\n@@ -215,6 +215,12 @@ empty.\n \t`branches`, all local branches that point to commits which are\n \tdescendants of the original commit will be rewritten. With `head`, only\n \tthe current `HEAD` reference will be rewritten. Defaults to `branches`.\n++\n+A branch that is used by another worktree, or that the current worktree is\n+in the middle of rebasing or bisecting, cannot be rewritten, as the index and\n+working tree of that worktree would not match the rewritten branch. With\n+`branches` the command refuses in that case, so use `head` to rewrite only\n+the current branch.\n \n EXAMPLES\n --------\ndiff --git a/builtin/history.c b/builtin/history.c\nindex 43ba76f5a1..90f00f676d 100644\n--- a/builtin/history.c\n+++ b/builtin/history.c\n@@ -2,6 +2,7 @@\n \n #include \"builtin.h\"\n #include \"advice.h\"\n+#include \"branch.h\"\n #include \"cache-tree.h\"\n #include \"commit.h\"\n #include \"commit-reach.h\"\n@@ -295,6 +296,62 @@ static int revwalk_contains_merges(struct repository *repo,\n \treturn ret;\n }\n \n+/*\n+ * Rewriting a branch that is used by another worktree moves the branch\n+ * without updating the index and working tree of that worktree. The same\n+ * goes for the branch that the current worktree is rebasing. Refuse to\n+ * rewrite such branches, but not the branch that the current worktree has\n+ * checked out, as we update that one ourselves.\n+ */\n+static int reject_checked_out_branches(struct repository *repo,\n+\t\t\t\t       struct commit *original)\n+{\n+\tstruct ref_filter filter = REF_FILTER_INIT;\n+\tstruct ref_array refs = { 0 };\n+\tchar *head_target;\n+\tint ret = 0;\n+\n+\thead_target = refs_resolve_refdup(get_main_ref_store(repo), \"HEAD\",\n+\t\t\t\t\t  RESOLVE_REF_NO_RECURSE, NULL, NULL);\n+\n+\tcommit_list_insert(original, &filter.with_commit);\n+\tfilter.kind = FILTER_REFS_BRANCHES;\n+\tif (filter_refs(&refs, &filter, filter.kind)) {\n+\t\tret = error(_(\"could not filter refs\"));\n+\t\tgoto out;\n+\t}\n+\n+\tfor (int i = 0; i < refs.nr; i++) {\n+\t\tconst char *refname = refs.items[i]->refname;\n+\t\tconst char *branch, *path;\n+\n+\t\tif (head_target && !strcmp(refname, head_target))\n+\t\t\tcontinue;\n+\n+\t\tpath = branch_checked_out(refname);\n+\t\tif (!path)\n+\t\t\tcontinue;\n+\n+\t\tif (!skip_prefix(refname, \"refs/heads/\", &branch))\n+\t\t\tBUG(\"branch filter returned '%s'\", refname);\n+\t\terror(_(\"cannot rewrite branch '%s' used by worktree at '%s'\"),\n+\t\t      branch, path);\n+\t\tret = -1;\n+\t}\n+\n+\tif (ret)\n+\t\tadvise_if_enabled(ADVICE_HISTORY_UPDATE_REFS,\n+\t\t\t\t  _(\"Use --update-refs=head to rewrite only \"\n+\t\t\t\t    \"the current branch and leave such branches \"\n+\t\t\t\t    \"untouched.\"));\n+\n+out:\n+\tfree(head_target);\n+\tref_filter_clear(&filter);\n+\tref_array_clear(&refs);\n+\treturn ret;\n+}\n+\n static int setup_revwalk(struct repository *repo,\n \t\t\t enum ref_action action,\n \t\t\t struct commit *original,\n@@ -350,6 +407,10 @@ static int setup_revwalk(struct repository *repo,\n \n \t\tstrvec_push(&args, \"HEAD\");\n \t} else {\n+\t\tret = reject_checked_out_branches(repo, original);\n+\t\tif (ret < 0)\n+\t\t\tgoto out;\n+\n \t\tstrvec_push(&args, \"--branches\");\n \t\tstrvec_push(&args, \"HEAD\");\n \t}\ndiff --git a/t/t3450-history.sh b/t/t3450-history.sh\nindex f513463b92..f7ca66edc6 100755\n--- a/t/t3450-history.sh\n+++ b/t/t3450-history.sh\n@@ -14,4 +14,132 @@ test_expect_success 'raises an error with unknown argument' '\n \ttest_grep \"unknown subcommand: .garbage.\" err\n '\n \n+test_expect_success 'refuses to rewrite a branch checked out in another worktree' '\n+\ttest_when_finished \"rm -rf repo wt\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit worktree add ../wt -b other &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop second 2>err &&\n+\t\ttest_grep \"^error: cannot rewrite branch .other. used by worktree at .*wt\" err &&\n+\t\ttest_grep \"^hint: .* --update-refs=head\" err &&\n+\t\ttest_grep ! \"branch .main.\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual\n+\t)\n+'\n+\n+test_expect_success 'refusing to rewrite checked out branches honors --dry-run' '\n+\ttest_when_finished \"rm -rf repo wt\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit worktree add ../wt -b other &&\n+\n+\t\ttest_must_fail git history drop --dry-run second >out 2>err &&\n+\t\ttest_grep \"^error: cannot rewrite branch .other. used by worktree at\" err &&\n+\t\ttest_must_be_empty out\n+\t)\n+'\n+\n+test_expect_success 'advice.historyUpdateRefs silences the hint for checked out branches' '\n+\ttest_when_finished \"rm -rf repo wt\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit worktree add ../wt -b other &&\n+\n+\t\ttest_must_fail git -c advice.historyUpdateRefs=false \\\n+\t\t\thistory drop second 2>err &&\n+\t\ttest_grep \"^error: cannot rewrite branch .other. used by worktree at\" err &&\n+\t\ttest_grep ! \"hint:\" err\n+\t)\n+'\n+\n+test_expect_success '--update-refs=head leaves branches of other worktrees alone' '\n+\ttest_when_finished \"rm -rf repo wt\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit worktree add ../wt -b other &&\n+\t\tgit rev-parse other >other-expect &&\n+\n+\t\tgit history drop --update-refs=head second &&\n+\n+\t\tgit rev-parse other >other-actual &&\n+\t\ttest_cmp other-expect other-actual &&\n+\t\tgit log --format=%s main >actual &&\n+\t\ttest_write_lines third first >expect &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'rewrites the branch of the current worktree' '\n+\ttest_when_finished \"rm -rf repo wt\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\tgit worktree add ../wt -b other &&\n+\t\ttest_commit -C ../wt second &&\n+\t\ttest_commit -C ../wt third &&\n+\n+\t\tgit -C ../wt history drop second &&\n+\n+\t\tgit log --format=%s other >actual &&\n+\t\ttest_write_lines third first >expect &&\n+\t\ttest_cmp expect actual\n+\t)\n+'\n+\n+test_expect_success 'refuses to rewrite a branch that is being rebased' '\n+\ttest_when_finished \"rm -rf repo\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\ttest_must_fail git rebase -x false first &&\n+\n+\t\tgit refs list >refs-expect &&\n+\t\ttest_must_fail git history drop second 2>err &&\n+\t\ttest_grep \"^error: cannot rewrite branch .main. used by worktree at\" err &&\n+\t\tgit refs list >refs-actual &&\n+\t\ttest_cmp refs-expect refs-actual &&\n+\n+\t\tgit rebase --abort\n+\t)\n+'\n+\n+test_expect_success 'refuses to rewrite a branch that is being bisected' '\n+\ttest_when_finished \"rm -rf repo wt\" &&\n+\tgit init repo --initial-branch=main &&\n+\t(\n+\t\tcd repo &&\n+\t\ttest_commit first &&\n+\t\ttest_commit second &&\n+\t\ttest_commit third &&\n+\t\tgit worktree add ../wt -b other &&\n+\t\tgit -C ../wt bisect start HEAD first &&\n+\n+\t\ttest_must_fail git history drop second 2>err &&\n+\t\ttest_grep \"^error: cannot rewrite branch .other. used by worktree at\" err\n+\t)\n+'\n+\n test_done\n\nbase-commit: f7de81456940dd6f3274e461149bba12965de609\n-- \n2.54.0 (Apple Git-157)\n\n\n"},{"id":"559143","messageId":"a0ebd98f-669e-4ff2-bca2-cfd5da5a7316@gmail.com","threadId":"66921","inReplyTo":"20261123103911.98860-1-aniketgupta3001@gmail.com","subject":"Re: [PATCH] history: refuse to rewrite branches used by other worktrees","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-23T13:10:50Z","receivedAt":"2026-11-23T13:10:50Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"Hi Aniket\n\nThanks for working on this, it is a useful improvement.\n\nOn 23/11/2026 10:39, Aniket Gupta wrote:\n> By default \"git history\" also rewrites every local branch that descends\n\n\"also\" is unnecessary here\n\n> from the commit it changes. If another worktree has such a branch\n> checked out, the branch moves but the index and working tree of that\n> worktree stay behind. So it shows staged changes that nobody made.\n\nThe 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.\n\n> If\n> the current worktree is in the middle of rebasing the branch, \"git\n> rebase --continue\" later fails because the branch is not where the\n> rebase expects it.\n> > \"git branch -f\" refuses to move such branches and \"git rebase\n> --update-refs\" skips them. Do the same here: before replaying anything\n> look for local branches that contain the commit and are used by a\n> worktree and refuse to go on if there is one. Use branch_checked_out()\n> so that a branch that is being rebased or bisected counts as used.\n\nGood. 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.\n\n> Skip\n> the branch that the current worktree has checked out, since the command\n> already takes care of the index and working tree of that worktree.\n\nWhat happens if a rebase is running in the same worktree as the user tries to run \"git history\"?\n\n> Refuse with --dry-run as well because it will show what a real run will\n> do.\n\nGood\n\n> Mention --update-refs=head, which leaves the other branches alone, with\n> the existing advice.historyUpdateRefs hint.\n\nDoes --update-refs=head try to update the current branch? If so we should check that is not being written somewhere else.\n\n> > diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc\n> index 3d91c90eda..bb0e24b745 100644\n> --- a/Documentation/config/advice.adoc\n> +++ b/Documentation/config/advice.adoc\n> @@ -60,9 +60,10 @@ all advice messages.\n>   \t\tShown when the user tries to delete a not fully merged\n>   \t\tbranch without the force option set.\n>   \thistoryUpdateRefs::\n> -\t\tShown when `git history squash` refuses because a local branch\n> -\t\tcannot be rewritten as a descendant of the squashed commit, to\n> -\t\ttell the user about `--update-refs=head`.\n> +\t\tShown when `git history` refuses because a local branch is used\n\nThis 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.\n\n\tShown when 'git history' refuses to run because ...\n\n> +\t\tby another worktree, or, for `squash`, because it cannot be\n> +\t\trewritten as a descendant of the squashed commit, to tell the\n> +\t\tuser about `--update-refs=head`.\n>   \tignoredHook::\n>   \t\tShown when a hook is ignored because the hook is not\n>   \t\tset as executable.\n> diff --git a/Documentation/git-history.adoc b/Documentation/git-history.adoc\n> index 1fcc30150a..ce7c0a88c8 100644\n> --- a/Documentation/git-history.adoc\n> +++ b/Documentation/git-history.adoc\n> @@ -215,6 +215,12 @@ empty.\n>   \t`branches`, all local branches that point to commits which are\n>   \tdescendants of the original commit will be rewritten. With `head`, only\n>   \tthe current `HEAD` reference will be rewritten. Defaults to `branches`.\n> ++\n> +A branch that is used by another worktree, or that the current worktree is\n> +in the middle of rebasing or bisecting, cannot be rewritten, as the index and\n> +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\n> +the current branch.\n\nWe should also prevent rebase from rewriting a branch that is being rewritten by \"git history\" but that can come later.\n\n> +/*\n> + * Rewriting a branch that is used by another worktree moves the branch\n> + * without updating the index and working tree of that worktree. The same\n> + * goes for the branch that the current worktree is rebasing. Refuse to\n> + * rewrite such branches, but not the branch that the current worktree has\n> + * checked out, as we update that one ourselves.\n> + */\n> +static int reject_checked_out_branches(struct repository *repo,\n> +\t\t\t\t       struct commit *original)\n> +{\n> +\tstruct ref_filter filter = REF_FILTER_INIT;\n> +\tstruct ref_array refs = { 0 };\n> +\tchar *head_target;\n> +\tint ret = 0;\n> +\n> +\thead_target = refs_resolve_refdup(get_main_ref_store(repo), \"HEAD\",\n> +\t\t\t\t\t  RESOLVE_REF_NO_RECURSE, NULL, NULL);\n> +\n> +\tcommit_list_insert(original, &filter.with_commit);\n> +\tfilter.kind = FILTER_REFS_BRANCHES;\n> +\tif (filter_refs(&refs, &filter, filter.kind)) {\n\nThis is correct, but it duplicates the revision walk we're already doing when looking for merge commits. It would be more efficient to\n\n(1) modify setup_revwalk() to load decorations (we already load them\n    when replaying the commits so there is no extra work, we're just\n    loading them earlier)\n(2) modify and rename revwalk_contains_merges() to\n  (a) drop\"--min-parents\" and check for merges by looking at\n      commit->parents->next.\n  (b) check DECORATION_REF_LOCAL decorations are not checked out\n      elsewhere.\n\nFor an existing example of using decorations, look at replay.c:replay_revisions().\n\nThanks\n\nPhillip\n\n> +\t\tret = error(_(\"could not filter refs\"));\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tfor (int i = 0; i < refs.nr; i++) {\n> +\t\tconst char *refname = refs.items[i]->refname;\n> +\t\tconst char *branch, *path;\n> +\n> +\t\tif (head_target && !strcmp(refname, head_target))\n> +\t\t\tcontinue;\n> +\n> +\t\tpath = branch_checked_out(refname);\n> +\t\tif (!path)\n> +\t\t\tcontinue;\n> +\n> +\t\tif (!skip_prefix(refname, \"refs/heads/\", &branch))\n> +\t\t\tBUG(\"branch filter returned '%s'\", refname);\n> +\t\terror(_(\"cannot rewrite branch '%s' used by worktree at '%s'\"),\n> +\t\t      branch, path);\n> +\t\tret = -1;\n> +\t}\n> +\n> +\tif (ret)\n> +\t\tadvise_if_enabled(ADVICE_HISTORY_UPDATE_REFS,\n> +\t\t\t\t  _(\"Use --update-refs=head to rewrite only \"\n> +\t\t\t\t    \"the current branch and leave such branches \"\n> +\t\t\t\t    \"untouched.\"));\n> +\n> +out:\n> +\tfree(head_target);\n> +\tref_filter_clear(&filter);\n> +\tref_array_clear(&refs);\n> +\treturn ret;\n> +}\n> +\n>   static int setup_revwalk(struct repository *repo,\n>   \t\t\t enum ref_action action,\n>   \t\t\t struct commit *original,\n> @@ -350,6 +407,10 @@ static int setup_revwalk(struct repository *repo,\n>   >   \t\tstrvec_push(&args, \"HEAD\");\n>   \t} else {\n> +\t\tret = reject_checked_out_branches(repo, original);\n> +\t\tif (ret < 0)\n> +\t\t\tgoto out;\n> +\n>   \t\tstrvec_push(&args, \"--branches\");\n>   \t\tstrvec_push(&args, \"HEAD\");\n>   \t}\n> diff --git a/t/t3450-history.sh b/t/t3450-history.sh\n> index f513463b92..f7ca66edc6 100755\n> --- a/t/t3450-history.sh\n> +++ b/t/t3450-history.sh\n> @@ -14,4 +14,132 @@ test_expect_success 'raises an error with unknown argument' '\n>   \ttest_grep \"unknown subcommand: .garbage.\" err\n>   '\n>   > +test_expect_success 'refuses to rewrite a branch checked out in another worktree' '\n> +\ttest_when_finished \"rm -rf repo wt\" &&\n> +\tgit init repo --initial-branch=main &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit first &&\n> +\t\ttest_commit second &&\n> +\t\ttest_commit third &&\n> +\t\tgit worktree add ../wt -b other &&\n> +\n> +\t\tgit refs list >refs-expect &&\n> +\t\ttest_must_fail git history drop second 2>err &&\n> +\t\ttest_grep \"^error: cannot rewrite branch .other. used by worktree at .*wt\" err &&\n> +\t\ttest_grep \"^hint: .* --update-refs=head\" err &&\n> +\t\ttest_grep ! \"branch .main.\" err &&\n> +\t\tgit refs list >refs-actual &&\n> +\t\ttest_cmp refs-expect refs-actual\n> +\t)\n> +'\n> +\n> +test_expect_success 'refusing to rewrite checked out branches honors --dry-run' '\n> +\ttest_when_finished \"rm -rf repo wt\" &&\n> +\tgit init repo --initial-branch=main &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit first &&\n> +\t\ttest_commit second &&\n> +\t\ttest_commit third &&\n> +\t\tgit worktree add ../wt -b other &&\n> +\n> +\t\ttest_must_fail git history drop --dry-run second >out 2>err &&\n> +\t\ttest_grep \"^error: cannot rewrite branch .other. used by worktree at\" err &&\n> +\t\ttest_must_be_empty out\n> +\t)\n> +'\n> +\n> +test_expect_success 'advice.historyUpdateRefs silences the hint for checked out branches' '\n> +\ttest_when_finished \"rm -rf repo wt\" &&\n> +\tgit init repo --initial-branch=main &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit first &&\n> +\t\ttest_commit second &&\n> +\t\ttest_commit third &&\n> +\t\tgit worktree add ../wt -b other &&\n> +\n> +\t\ttest_must_fail git -c advice.historyUpdateRefs=false \\\n> +\t\t\thistory drop second 2>err &&\n> +\t\ttest_grep \"^error: cannot rewrite branch .other. used by worktree at\" err &&\n> +\t\ttest_grep ! \"hint:\" err\n> +\t)\n> +'\n> +\n> +test_expect_success '--update-refs=head leaves branches of other worktrees alone' '\n> +\ttest_when_finished \"rm -rf repo wt\" &&\n> +\tgit init repo --initial-branch=main &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit first &&\n> +\t\ttest_commit second &&\n> +\t\ttest_commit third &&\n> +\t\tgit worktree add ../wt -b other &&\n> +\t\tgit rev-parse other >other-expect &&\n> +\n> +\t\tgit history drop --update-refs=head second &&\n> +\n> +\t\tgit rev-parse other >other-actual &&\n> +\t\ttest_cmp other-expect other-actual &&\n> +\t\tgit log --format=%s main >actual &&\n> +\t\ttest_write_lines third first >expect &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success 'rewrites the branch of the current worktree' '\n> +\ttest_when_finished \"rm -rf repo wt\" &&\n> +\tgit init repo --initial-branch=main &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit first &&\n> +\t\tgit worktree add ../wt -b other &&\n> +\t\ttest_commit -C ../wt second &&\n> +\t\ttest_commit -C ../wt third &&\n> +\n> +\t\tgit -C ../wt history drop second &&\n> +\n> +\t\tgit log --format=%s other >actual &&\n> +\t\ttest_write_lines third first >expect &&\n> +\t\ttest_cmp expect actual\n> +\t)\n> +'\n> +\n> +test_expect_success 'refuses to rewrite a branch that is being rebased' '\n> +\ttest_when_finished \"rm -rf repo\" &&\n> +\tgit init repo --initial-branch=main &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit first &&\n> +\t\ttest_commit second &&\n> +\t\ttest_commit third &&\n> +\t\ttest_must_fail git rebase -x false first &&\n> +\n> +\t\tgit refs list >refs-expect &&\n> +\t\ttest_must_fail git history drop second 2>err &&\n> +\t\ttest_grep \"^error: cannot rewrite branch .main. used by worktree at\" err &&\n> +\t\tgit refs list >refs-actual &&\n> +\t\ttest_cmp refs-expect refs-actual &&\n> +\n> +\t\tgit rebase --abort\n> +\t)\n> +'\n> +\n> +test_expect_success 'refuses to rewrite a branch that is being bisected' '\n> +\ttest_when_finished \"rm -rf repo wt\" &&\n> +\tgit init repo --initial-branch=main &&\n> +\t(\n> +\t\tcd repo &&\n> +\t\ttest_commit first &&\n> +\t\ttest_commit second &&\n> +\t\ttest_commit third &&\n> +\t\tgit worktree add ../wt -b other &&\n> +\t\tgit -C ../wt bisect start HEAD first &&\n> +\n> +\t\ttest_must_fail git history drop second 2>err &&\n> +\t\ttest_grep \"^error: cannot rewrite branch .other. used by worktree at\" err\n> +\t)\n> +'\n> +\n>   test_done\n> > base-commit: f7de81456940dd6f3274e461149bba12965de609\n\n\n"},{"id":"559183","messageId":"xmqqa4mz5mr8.fsf@gitster.g","threadId":"66921","inReplyTo":"20261123103911.98860-1-aniketgupta3001@gmail.com","subject":"Re: [PATCH] history: refuse to rewrite branches used by other worktrees","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2026-11-23T20:14:03Z","receivedAt":"2026-11-23T20:14:03Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"Aniket Gupta <aniketgupta3001@gmail.com> writes:\n\n> +/*\n> + * Rewriting a branch that is used by another worktree moves the branch\n> + * without updating the index and working tree of that worktree. The same\n> + * goes for the branch that the current worktree is rebasing. Refuse to\n> + * rewrite such branches, but not the branch that the current worktree has\n> + * checked out, as we update that one ourselves.\n> + */\n\nThe intent is good.\n\n> +static int reject_checked_out_branches(struct repository *repo,\n> +\t\t\t\t       struct commit *original)\n> +{\n> +\tstruct ref_filter filter = REF_FILTER_INIT;\n> +\tstruct ref_array refs = { 0 };\n> +\tchar *head_target;\n> +\tint ret = 0;\n> +\n> +\thead_target = refs_resolve_refdup(get_main_ref_store(repo), \"HEAD\",\n> +\t\t\t\t\t  RESOLVE_REF_NO_RECURSE, NULL, NULL);\n> +\n> +\tcommit_list_insert(original, &filter.with_commit);\n> +\tfilter.kind = FILTER_REFS_BRANCHES;\n> +\tif (filter_refs(&refs, &filter, filter.kind)) {\n> +\t\tret = error(_(\"could not filter refs\"));\n> +\t\tgoto out;\n> +\t}\n> +\n> +\tfor (int i = 0; i < refs.nr; i++) {\n> +\t\tconst char *refname = refs.items[i]->refname;\n> +\t\tconst char *branch, *path;\n> +\n> +\t\tif (head_target && !strcmp(refname, head_target))\n> +\t\t\tcontinue;\n\nImagine we have a bare repository repo.git whose primary branch is\n'main'.  Its HEAD records refs/heads/main (which is a common state\nafter you run \"git clone --bare\" from a modern project whose primary\nbranch is 'main').  Then you can add a worktree ../wt to check out the\n'main' branch, because branch_checked_out() will say \"no\" when you\ncreate and check out 'main' in that new worktree.\n\nIn that setup, what would running \"git history\" in the bare repository\nto, say, drop a commit, do?  The 'main' branch is checked out\nelsewhere in the worktree at ../wt; we run \"git history\" in the bare\nrepository, and head_target above is resolved to \"refs/heads/main\".\nThis 'continue' triggers for the 'main' branch, so we return ret == 0.\n\n> +\t\tpath = branch_checked_out(refname);\n> +\t\tif (!path)\n> +\t\t\tcontinue;\n> +\n> +\t\tif (!skip_prefix(refname, \"refs/heads/\", &branch))\n> +\t\t\tBUG(\"branch filter returned '%s'\", refname);\n> +\t\terror(_(\"cannot rewrite branch '%s' used by worktree at '%s'\"),\n> +\t\t      branch, path);\n> +\t\tret = -1;\n> +\t}\n> +\n> +\tif (ret)\n> +\t\tadvise_if_enabled(ADVICE_HISTORY_UPDATE_REFS,\n> +\t\t\t\t  _(\"Use --update-refs=head to rewrite only \"\n> +\t\t\t\t    \"the current branch and leave such branches \"\n> +\t\t\t\t    \"untouched.\"));\n> +\n> +out:\n> +\tfree(head_target);\n> +\tref_filter_clear(&filter);\n> +\tref_array_clear(&refs);\n> +\treturn ret;\n> +}\n\nNow, the following is all outside the context of this patch, but\nlooking at cmd_history_drop(), it calls setup_revwalk() which in turn\ncalls the above function, which does not complain in this case.\n\nAnd is_bare_repository() is true.  We skip update_worktree() and\nupdate the 'main' branch via its HEAD, making the index and the\nworktree of the worktree at ../wt out of sync, no?\n\nPerhaps head_target should be initialized to NULL and assigned the\nresolution result of HEAD only in a non-bare repository, or something?\nI didn't trace all other callers, so there may be other glitches I did\nnot notice.\n\nThe proposed log message claims to \"Skip the branch that the current\nworktree has checked out, since the command already takes care of the\nindex and working tree of that worktree\", but as we saw above, it is\ninaccurate for bare repositories.  It probably is also worth noting in\nthe log message that 'drop' updates the worktree while 'reword',\n'split', and 'squash' preserve the tip tree and 'fixup' incorporates\nstaged changes from the index.\n\nThanks, I think this patch still needs some work.\n\n\n"},{"id":"559190","messageId":"20261123215424.27553-1-aniketgupta3001@gmail.com","threadId":"66921","inReplyTo":"xmqqa4mz5mr8.fsf@gitster.g","subject":"Re: [PATCH] history: refuse to rewrite branches used by other worktrees","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-23T21:54:24Z","receivedAt":"2026-11-23T21:54:24Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"Thanks for taking a look!\n\n> And is_bare_repository() is true.  We skip update_worktree() and\n> update the 'main' branch via its HEAD, making the index and the\n> worktree of the worktree at ../wt out of sync, no?\n\nTrue. I am able to repro this. In a bare repo nobody has the branch that\nHEAD points to checked out, but the check skips it as if the current\nworktree did. So \"git history drop\" moves \"main\" and ../wt shows the\nchanges of the dropped commit as staged.\n\n> Perhaps head_target should be initialized to NULL and assigned the\n> resolution result of HEAD only in a non-bare repository, or something?\n\nI think that would fix it. In v2 I'd like to go a bit further though:\nask the other worktrees if they use the current branch instead of\nskipping it by name. That also covers a bare repo and a branch that two\nworktrees have checked out with \"git worktree add --force\". Will add\ntests for both.\n\n> I didn't trace all other callers, so there may be other glitches I\n> did not notice.\n\nWent through them. fixup, reword, split, squash, drop all call\nsetup_revwalk() before they open an editor, create a commit or update a\nref so they refuse early. Will add a test that runs each of them.\n\n> It probably is also worth noting in\n> the log message that 'drop' updates the worktree while 'reword',\n> 'split', and 'squash' preserve the tip tree and 'fixup' incorporates\n> staged changes from the index.\n\nPoint taken. Will fix the log message and add that.\n\n"},{"id":"559191","messageId":"20261123215431.27574-1-aniketgupta3001@gmail.com","threadId":"66921","inReplyTo":"a0ebd98f-669e-4ff2-bca2-cfd5da5a7316@gmail.com","subject":"Re: [PATCH] history: refuse to rewrite branches used by other worktrees","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-23T21:54:31Z","receivedAt":"2026-11-23T21:54:31Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"Thanks for taking a look!\n\n> \"also\" is unnecessary here\n\nWill drop this.\n\n> The staged changes it shows were made by \"git history\" - they did not\n> appear from nowhere. An alternative to refusing to run would be to\n> update those worktrees if they are clean, but failing is the simpler\n> option to implement.\n\nFair point, will reword. I stayed with refusing as it is simpler.\nUpdating clean worktrees could come later if people want it.\n\n> fail to notice it is checked out elsewhere. That's not a problem that\n> needs solving here though.\n\nNoted. For the current branch v2 asks each worktree, so it does not\ndepend on branch_checked_out() there. The other branches still go\nthrough it.\n\n> What happens if a rebase is running in the same worktree as the user\n> tries to run \"git history\"?\n\nIt is refused. HEAD is detached then, so the branch being rebased is not\nthe one this worktree has checked out, and it gets treated like any\nother branch in use. There is a test for it, but the commit message does\nnot contain this. Will fix.\n\nIn that case --update-refs=head is not refused: it only rewrites the\ndetached HEAD, same as before. I tried fixup, reword, drop and squash\nthat way, and \"git rebase --continue\" finished fine each time.\n\n> Does --update-refs=head try to update the current branch? If so we\n> should check that is not being written somewhere else.\n\nYes, it updates the branch HEAD points to and nothing checks it today.\nI could repro it with \"git worktree add --force\" on the same\nbranch: after a fixup in one of them, the other shows a staged change.\nGood catch, will add the check.\n\n> we should say what \"git history\" refuses to do.\n\nWill fix.\n\n> We should also prevent rebase from rewriting a branch that is being\n> rewritten by \"git history\" but that can come later.\n\nAgreed, that can come later.\n\n> This is correct, but it duplicates the revision walk we're already\n> doing when looking for merge commits.\n\nMakes sense. I gave it a try and it works. One point: the branch\npointing at the rewritten commit itself is not part of the walk, so I\nlook at its decorations too. Rewriting a commit that another worktree's\nbranch points at goes through without it. Will do it like this in v2.\n\n"},{"id":"559224","messageId":"14c971ef-438c-4197-b0cd-df9bf0ef0ae4@gmail.com","threadId":"66921","inReplyTo":"20261123215431.27574-1-aniketgupta3001@gmail.com","subject":"Re: [PATCH] history: refuse to rewrite branches used by other worktrees","fromName":"Phillip Wood","fromEmail":"phillip.wood123@gmail.com","sentAt":"2026-11-24T10:49:42Z","receivedAt":"2026-11-24T10:49:42Z","isPatch":true,"sender":{"key":"phillip.wood@dunelm.org.uk","avatar":null},"body":"On 23/11/2026 21:54, Aniket Gupta wrote:\n> Thanks for taking a look!\n\nYou're welcome, thanks for working on it.\n\n>> What happens if a rebase is running in the same worktree as the user\n>> tries to run \"git history\"?\n> > It is refused. HEAD is detached then, so the branch being rebased is not\n> the one this worktree has checked out, and it gets treated like any\n> other branch in use. There is a test for it, but the commit message does\n> not contain this. Will fix.\n> > In that case --update-refs=head is not refused: it only rewrites the\n> detached HEAD, same as before. I tried fixup, reword, drop and squash\n> that way, and \"git rebase --continue\" finished fine each time.\n\nThat 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.\n\n> >> We should also prevent rebase from rewriting a branch that is being\n>> rewritten by \"git history\" but that can come later.\n> > Agreed, that can come later.\n\nwe should also refuse to run if another \"git history\" is rewriting the same branches - that's also something for another time.\n\n>> This is correct, but it duplicates the revision walk we're already\n>> doing when looking for merge commits.\n> > Makes sense. I gave it a try and it works. One point: the branch\n> pointing at the rewritten commit itself is not part of the walk, so I\n> look at its decorations too.\n\nGreat, I realized that after I sent my mail yesterday, well spotted.\n\nThanks\n\nPhillip\n\n> Rewriting a commit that another worktree's\n> branch points at goes through without it. Will do it like this in v2.\n\n\n"},{"id":"559227","messageId":"20261124140905.47213-1-aniketgupta3001@gmail.com","threadId":"66921","inReplyTo":"14c971ef-438c-4197-b0cd-df9bf0ef0ae4@gmail.com","subject":"Re: [PATCH] history: refuse to rewrite branches used by other worktrees","fromName":"Aniket Gupta","fromEmail":"aniketgupta3001@gmail.com","sentAt":"2026-11-24T14:09:05Z","receivedAt":"2026-11-24T14:09:05Z","isPatch":true,"sender":{"key":"aniketgupta3001@gmail.com","avatar":null},"body":"> That sounds like a bug though, because \"git history\" will update\n> HEAD in the middle of a rebase and we throw away the result of\n> running \"git history\" when \"git rebase\" completes.\n\nIt was wrong to say it is refused. That only happens when the commit\nis still on the branch being rebased. My bad. Otherwise, the rewrite\nis gone after \"git rebase --abort\". Will refuse in both modes.\n\n"}]}