From: Aniket Gupta Date: Mon, 23 Nov 2026 10:39:11 GMT Subject: [PATCH] history: refuse to rewrite branches used by other worktrees Message-ID: <20261123103911.98860-1-aniketgupta3001@gmail.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 Reported-by: Nikita Bobko Signed-off-by: Aniket Gupta --- 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)