* [PATCH] history: refuse to rewrite branches used by other worktrees
@ 2026-11-23 10:39 Aniket Gupta
2026-11-23 13:10 ` Phillip Wood
` (2 more replies)
0 siblings, 3 replies; 16+ messages in thread
From: Aniket Gupta @ 2026-11-23 10:39 UTC (permalink / raw)
To: git
Cc: Patrick Steinhardt, Toon Claes, Junio C Hamano, Phillip Wood,
Nikita Bobko
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)
^ permalink raw reply related [flat|nested] 16+ messages in thread* Re: [PATCH] history: refuse to rewrite branches used by other worktrees 2026-11-23 10:39 [PATCH] history: refuse to rewrite branches used by other worktrees Aniket Gupta @ 2026-11-23 13:10 ` Phillip Wood 2026-11-23 21:54 ` Aniket Gupta 2026-11-23 20:14 ` Junio C Hamano 2026-11-27 17:32 ` [PATCH v2] " Aniket Gupta 2 siblings, 1 reply; 16+ messages in thread From: Phillip Wood @ 2026-11-23 13:10 UTC (permalink / raw) To: Aniket Gupta, git Cc: Patrick Steinhardt, Toon Claes, Junio C Hamano, Phillip Wood, Nikita Bobko 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 ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH] history: refuse to rewrite branches used by other worktrees 2026-11-23 13:10 ` Phillip Wood @ 2026-11-23 21:54 ` Aniket Gupta 2026-11-24 10:49 ` Phillip Wood 0 siblings, 1 reply; 16+ messages in thread From: Aniket Gupta @ 2026-11-23 21:54 UTC (permalink / raw) To: Phillip Wood Cc: git, Patrick Steinhardt, Toon Claes, Junio C Hamano, Phillip Wood, Nikita Bobko 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. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH] history: refuse to rewrite branches used by other worktrees 2026-11-23 21:54 ` Aniket Gupta @ 2026-11-24 10:49 ` Phillip Wood 2026-11-24 14:09 ` Aniket Gupta 0 siblings, 1 reply; 16+ messages in thread From: Phillip Wood @ 2026-11-24 10:49 UTC (permalink / raw) To: Aniket Gupta Cc: git, Patrick Steinhardt, Toon Claes, Junio C Hamano, Phillip Wood, Nikita Bobko 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. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH] history: refuse to rewrite branches used by other worktrees 2026-11-24 10:49 ` Phillip Wood @ 2026-11-24 14:09 ` Aniket Gupta 0 siblings, 0 replies; 16+ messages in thread From: Aniket Gupta @ 2026-11-24 14:09 UTC (permalink / raw) To: Phillip Wood Cc: git, Patrick Steinhardt, Toon Claes, Junio C Hamano, Phillip Wood, Nikita Bobko > 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. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH] history: refuse to rewrite branches used by other worktrees 2026-11-23 10:39 [PATCH] history: refuse to rewrite branches used by other worktrees Aniket Gupta 2026-11-23 13:10 ` Phillip Wood @ 2026-11-23 20:14 ` Junio C Hamano 2026-11-23 21:54 ` Aniket Gupta 2026-11-27 17:32 ` [PATCH v2] " Aniket Gupta 2 siblings, 1 reply; 16+ messages in thread From: Junio C Hamano @ 2026-11-23 20:14 UTC (permalink / raw) To: Aniket Gupta Cc: git, Patrick Steinhardt, Toon Claes, Phillip Wood, Nikita Bobko 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. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH] history: refuse to rewrite branches used by other worktrees 2026-11-23 20:14 ` Junio C Hamano @ 2026-11-23 21:54 ` Aniket Gupta 0 siblings, 0 replies; 16+ messages in thread From: Aniket Gupta @ 2026-11-23 21:54 UTC (permalink / raw) To: Junio C Hamano Cc: git, Patrick Steinhardt, Toon Claes, Phillip Wood, Nikita Bobko 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. ^ permalink raw reply [flat|nested] 16+ messages in thread
* [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-23 10:39 [PATCH] history: refuse to rewrite branches used by other worktrees Aniket Gupta 2026-11-23 13:10 ` Phillip Wood 2026-11-23 20:14 ` Junio C Hamano @ 2026-11-27 17:32 ` Aniket Gupta 2026-11-28 0:12 ` Junio C Hamano ` (2 more replies) 2 siblings, 3 replies; 16+ messages in thread From: Aniket Gupta @ 2026-11-27 17:32 UTC (permalink / raw) To: git Cc: Patrick Steinhardt, Toon Claes, Junio C Hamano, Phillip Wood, Nikita Bobko By default "git history" 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 the rewrite as staged changes. 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, in any worktree, counts as used. Updating clean worktrees would be another way, but refusing is simpler. Refuse with --dry-run as well because it will show what a real run will do. The branch that the current worktree has checked out is fine, since the command already takes care of the index and working tree of that worktree: "drop" updates them, "reword", "split" and "squash" keep the tree of the tip, and "fixup" takes the staged changes. A bare repository has no such worktree, and branch_checked_out() records only one worktree per branch, so ask the other worktrees whether they use the branch instead of skipping it by name. --update-refs=head refuses in the same cases. Refuse to rewrite HEAD while a rebase is in progress, with either --update-refs mode, since "git rebase --abort" would throw the result away. 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> Helped-by: Phillip Wood <phillip.wood123@gmail.com> Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com> --- Changes in v2: - Check the branch of the current worktree against the other worktrees instead of skipping it by name. This fixes a bare repository and a branch that two worktrees have checked out (Junio, Phillip). - Find the branches with the decorations of the revision walk instead of a second walk (Phillip). - Refuse to rewrite HEAD while a rebase is in progress (Phillip). - Reword the log message and advice.adoc. Documentation/config/advice.adoc | 7 +- Documentation/git-history.adoc | 7 + builtin/history.c | 144 ++++++++++++++++- t/t3450-history.sh | 265 +++++++++++++++++++++++++++++++ 4 files changed, 413 insertions(+), 10 deletions(-) diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc index 3d91c90eda..65f3ff0e02 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 to run 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..243817d28a 100644 --- a/Documentation/git-history.adoc +++ b/Documentation/git-history.adoc @@ -215,6 +215,13 @@ 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. The command refuses to rewrite `HEAD` while a rebase +is in progress in the current worktree. EXAMPLES -------- diff --git a/builtin/history.c b/builtin/history.c index 43ba76f5a1..d0a3287c1d 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" @@ -11,6 +12,7 @@ #include "gettext.h" #include "hex.h" #include "lockfile.h" +#include "log-tree.h" #include "merge-ort.h" #include "oidmap.h" #include "parse-options.h" @@ -27,6 +29,7 @@ #include "tree.h" #include "tree-walk.h" #include "unpack-trees.h" +#include "worktree.h" #include "wt-status.h" #define GIT_HISTORY_DROP_USAGE \ @@ -260,15 +263,100 @@ static int parse_ref_action(const struct option *opt, const char *value, int uns return 0; } -static int revwalk_contains_merges(struct repository *repo, - const struct strvec *revwalk_args) +/* + * Return the path of the worktree that uses "refname", if rewriting it would + * leave the index and working tree of that worktree behind. The branch that + * the current worktree has checked out is fine, unless another worktree has + * it checked out as well. + */ +static char *branch_used_elsewhere(struct repository *repo, + const char *refname, + const char *head_target) +{ + char *path = NULL; + + if (head_target && !strcmp(refname, head_target)) { + struct worktree **worktrees = get_worktrees(repo); + + for (int i = 0; worktrees[i]; i++) { + if (!worktrees[i]->is_current && + is_shared_symref(worktrees[i], "HEAD", refname)) { + path = xstrdup(worktrees[i]->path); + break; + } + } + free_worktrees(worktrees); + } else { + const char *checked_out = branch_checked_out(refname); + + if (checked_out) + path = xstrdup(checked_out); + } + + return path; +} + +/* "git rebase --abort" would throw away a rewrite of the detached HEAD. */ +static bool rebase_in_progress(struct repository *repo) +{ + struct worktree **worktrees = get_worktrees(repo); + struct wt_status_state state = { 0 }; + bool ret = false; + + for (int i = 0; worktrees[i]; i++) { + if (worktrees[i]->is_current) { + wt_status_check_rebase(worktrees[i], &state); + ret = state.rebase_in_progress || + state.rebase_interactive_in_progress; + break; + } + } + + wt_status_state_free_buffers(&state); + free_worktrees(worktrees); + return ret; +} + +static void collect_used_branches(struct repository *repo, + const struct commit *commit, + const char *head_target, + struct string_list *used, + bool *rewrites_head) { + const struct name_decoration *decoration; + + for (decoration = get_name_decoration(&commit->object); + decoration; + decoration = decoration->next) { + char *path; + + if (decoration->type == DECORATION_REF_HEAD) + *rewrites_head = true; + if (decoration->type != DECORATION_REF_LOCAL) + continue; + path = branch_used_elsewhere(repo, decoration->name, head_target); + if (path) + string_list_append(used, decoration->name)->util = path; + } +} + +static int validate_revwalk(struct repository *repo, + enum ref_action action, + struct commit *original, + const struct strvec *revwalk_args) +{ + struct string_list used = STRING_LIST_INIT_DUP; struct strvec args = STRVEC_INIT; struct rev_info revs; + struct commit *commit; + bool rewrites_head = action == REF_ACTION_HEAD; + char *head_target; int ret; + head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD", + 0, NULL, NULL); + strvec_pushv(&args, revwalk_args->v); - strvec_push(&args, "--min-parents=2"); repo_init_revisions(repo, &revs, NULL); @@ -281,17 +369,59 @@ static int revwalk_contains_merges(struct repository *repo, goto out; } - if (get_revision(&revs)) { - ret = error(_("replaying merge commits is not supported yet!")); + if (action == REF_ACTION_BRANCHES) + collect_used_branches(repo, original, head_target, &used, + &rewrites_head); + + while ((commit = get_revision(&revs))) { + if (commit->parents && commit->parents->next) { + ret = error(_("replaying merge commits is not supported yet!")); + goto out; + } + if (action == REF_ACTION_BRANCHES) + collect_used_branches(repo, commit, head_target, &used, + &rewrites_head); + } + + if (action == REF_ACTION_HEAD && head_target && + starts_with(head_target, "refs/heads/")) { + char *path = branch_used_elsewhere(repo, head_target, head_target); + if (path) + string_list_append(&used, head_target)->util = path; + } + + if (used.nr) { + string_list_sort(&used); + for (size_t i = 0; i < used.nr; i++) { + const char *branch; + + if (!skip_prefix(used.items[i].string, "refs/heads/", &branch)) + BUG("branch decoration without refs/heads/ prefix"); + error(_("cannot rewrite branch '%s' used by worktree at '%s'"), + branch, (const char *)used.items[i].util); + } + if (action == REF_ACTION_BRANCHES) + advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS, + _("Use --update-refs=head to rewrite only " + "the current branch and leave such branches " + "untouched.")); + ret = -1; + goto out; + } + + if (rewrites_head && rebase_in_progress(repo)) { + ret = error(_("cannot rewrite HEAD while a rebase is in progress")); goto out; } - reset_revision_walk(); ret = 0; out: + reset_revision_walk(); release_revisions(&revs); + string_list_clear(&used, 1); strvec_clear(&args); + free(head_target); return ret; } @@ -354,7 +484,7 @@ static int setup_revwalk(struct repository *repo, strvec_push(&args, "HEAD"); } - ret = revwalk_contains_merges(repo, &args); + ret = validate_revwalk(repo, action, original, &args); if (ret < 0) goto out; diff --git a/t/t3450-history.sh b/t/t3450-history.sh index f513463b92..03bd375ef8 100755 --- a/t/t3450-history.sh +++ b/t/t3450-history.sh @@ -14,4 +14,269 @@ 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 && + 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 && + git refs list >refs-actual && + test_cmp refs-expect refs-actual + ) +' + +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_expect_success 'refuses to rewrite HEAD while a rebase is in progress' ' + test_when_finished "rm -rf repo" && + git init repo --initial-branch=main && + ( + cd repo && + test_commit base && + git checkout -b topic && + test_commit topic-one && + test_commit topic-two && + git checkout main && + test_commit main-new && + git checkout topic && + git rev-parse topic >orig && + test_must_fail git rebase -x false main && + git rev-parse HEAD >head-expect && + + test_must_fail git history drop HEAD~1 2>err && + test_grep "^error: cannot rewrite HEAD while a rebase is in progress" err && + test_must_fail git history drop --update-refs=head HEAD 2>err && + test_grep "^error: cannot rewrite HEAD while a rebase is in progress" err && + git rev-parse HEAD >head-actual && + test_cmp head-expect head-actual && + + git rebase --abort && + git rev-parse topic >actual && + test_cmp orig actual + ) +' + +test_expect_success 'rewrites commits unrelated to HEAD while a rebase is in progress' ' + test_when_finished "rm -rf repo" && + git init repo --initial-branch=main && + ( + cd repo && + test_commit base && + git checkout -b other && + test_commit other-one && + test_commit other-two && + git checkout -b topic base && + test_commit topic-one && + git checkout main && + test_commit main-new && + git checkout topic && + test_must_fail git rebase -x false main && + git rev-parse HEAD >head-expect && + + git history drop other-one && + git log --format=%s other >actual && + test_write_lines other-two base >expect && + test_cmp expect actual && + git rev-parse HEAD >head-actual && + test_cmp head-expect head-actual && + git rebase --abort + ) +' + +test_expect_success 'refuses to rewrite the branch a bare repository names in HEAD when a worktree uses it' ' + test_when_finished "rm -rf repo repo.git wt" && + git init repo --initial-branch=main && + test_commit -C repo first && + test_commit -C repo second && + test_commit -C repo third && + git clone --bare repo repo.git && + git -C repo.git worktree add ../wt main && + + git -C repo.git refs list >refs-expect && + test_must_fail git -C repo.git history drop second 2>err && + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && + test_must_fail git -C repo.git history drop --update-refs=head second 2>err && + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && + git -C repo.git refs list >refs-actual && + test_cmp refs-expect refs-actual +' + +test_expect_success 'refuses to rewrite a branch that two worktrees have checked out' ' + 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 --force ../wt main && + + 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 .*wt" err && + test_must_fail git -C ../wt history drop second 2>err && + test_grep "^error: cannot rewrite branch .main. used by worktree at .*repo" err && + test_must_fail git history drop --update-refs=head second 2>err && + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && + test_grep ! "^hint:" err && + git refs list >refs-actual && + test_cmp refs-expect refs-actual + ) +' + +test_expect_success 'refuses to rewrite the commit that a branch of another worktree points at' ' + 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 second && + + git refs list >refs-expect && + test_must_fail git history reword second 2>err && + test_grep "^error: cannot rewrite branch .other. used by worktree at .*wt" err && + test_grep ! "branch .main." err && + git refs list >refs-actual && + test_cmp refs-expect refs-actual + ) +' + +test_expect_success 'every subcommand refuses before it does anything' ' + test_when_finished "rm -rf repo wt" && + git init repo --initial-branch=main && + ( + cd repo && + test_commit first && + test_commit second && + test_commit third && + test_commit fourth && + git worktree add ../wt -b other && + echo changed >first.t && + git add first.t && + write_script fail-editor <<-\EOF && + >editor-ran + exit 1 + EOF + test_set_editor "$(pwd)/fail-editor" && + + git refs list >refs-expect && + git diff --cached >index-expect && + for cmd in "drop second" "reword second" "split second" \ + "fixup second" "squash first..third" + do + test_must_fail git history $cmd </dev/null 2>err && + test_grep "^error: cannot rewrite branch .other. used by worktree at" err && + test_path_is_missing editor-ran && + git refs list >refs-actual && + test_cmp refs-expect refs-actual && + git diff --cached >index-actual && + test_cmp index-expect index-actual || return 1 + done + ) +' + test_done -- 2.54.0 (Apple Git-157) ^ permalink raw reply related [flat|nested] 16+ messages in thread
* Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-27 17:32 ` [PATCH v2] " Aniket Gupta @ 2026-11-28 0:12 ` Junio C Hamano 2026-11-28 7:37 ` Aniket Gupta 2026-11-30 11:31 ` Toon Claes 2026-11-30 14:49 ` Phillip Wood 2 siblings, 1 reply; 16+ messages in thread From: Junio C Hamano @ 2026-11-28 0:12 UTC (permalink / raw) To: Aniket Gupta Cc: git, Patrick Steinhardt, Toon Claes, Phillip Wood, Nikita Bobko Aniket Gupta <aniketgupta3001@gmail.com> writes: > ... A bare repository > has no such worktree, and branch_checked_out() records only one worktree > per branch, so ask the other worktrees whether they use the branch > instead of skipping it by name. --update-refs=head refuses in the same > cases. Checking the other worktrees for the current HEAD target instead of skipping it by name is a neat way to handle both bare repositories and branches forced into multiple worktrees. I had to read "--update-refs=head refuses in the same cases" above a few times to unconfuse myself. I initially took it to mean that it rejects whenever "--update-refs=branches" would reject, but "--update-refs=head" specifically leaves unrelated branches in other worktrees alone and only refuses when HEAD's target branch itself is used elsewhere (either checked out or when a rebase or bisect is in progress). Spelling that out directly would make the paragraph easier to follow. > diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc > index 3d91c90eda..65f3ff0e02 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 to run 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`. As tested in t/t3450-history.sh, when a branch is being rebased in the current worktree, this advice is also shown when the current worktree is rebasing or bisecting the branch with HEAD detached, not only when it is used by another worktree. > diff --git a/builtin/history.c b/builtin/history.c > index 43ba76f5a1..d0a3287c1d 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" > @@ -11,6 +12,7 @@ > #include "gettext.h" > #include "hex.h" > #include "lockfile.h" > +#include "log-tree.h" What new things from log-tree.h are referenced by this patch? The file seems to compile without this new inclusion. > @@ -260,15 +263,100 @@ static int parse_ref_action(const struct option *opt, const char *value, int uns > return 0; > } > > -static int revwalk_contains_merges(struct repository *repo, > - const struct strvec *revwalk_args) > +/* > + * Return the path of the worktree that uses "refname", if rewriting it would > + * leave the index and working tree of that worktree behind. The branch that > + * the current worktree has checked out is fine, unless another worktree has > + * it checked out as well. > + */ > +static char *branch_used_elsewhere(struct repository *repo, > + const char *refname, > + const char *head_target) > +{ > + char *path = NULL; > + > + if (head_target && !strcmp(refname, head_target)) { > + struct worktree **worktrees = get_worktrees(repo); > + > + for (int i = 0; worktrees[i]; i++) { > + if (!worktrees[i]->is_current && > + is_shared_symref(worktrees[i], "HEAD", refname)) { > + path = xstrdup(worktrees[i]->path); > + break; > + } > + } > + free_worktrees(worktrees); > + } else { > + const char *checked_out = branch_checked_out(refname); > + > + if (checked_out) > + path = xstrdup(checked_out); > + } > + > + return path; > +} This separation makes sense, given how branch_checked_out() works in branch.c. Because branch.c:prepare_checked_out_branches() populates the current_checked_out_branches strmap, a branch checked out in two worktrees only keeps one path in the map, so branch_checked_out() cannot tell whether another worktree also uses head_target. Asking non-current worktrees via is_shared_symref() covers symrefs, detached rebases, and bisects in other worktrees, and also works when run in a bare repository where the current worktree is bare. > +/* "git rebase --abort" would throw away a rewrite of the detached HEAD. */ > +static bool rebase_in_progress(struct repository *repo) > +{ > + struct worktree **worktrees = get_worktrees(repo); > + struct wt_status_state state = { 0 }; > + bool ret = false; > + > + for (int i = 0; worktrees[i]; i++) { > + if (worktrees[i]->is_current) { > + wt_status_check_rebase(worktrees[i], &state); > + ret = state.rebase_in_progress || > + state.rebase_interactive_in_progress; > + break; > + } > + } Instead of enumerating every worktree with get_worktrees() just to pick out '.is_current', we could call get_current_worktree(repo) and free_worktree(), as wt-status.c does in wt_status_get_state(). It would avoid reading HEAD for all linked worktrees, no? > @@ -281,17 +369,59 @@ static int revwalk_contains_merges(struct repository *repo, > goto out; > } > > - if (get_revision(&revs)) { > - ret = error(_("replaying merge commits is not supported yet!")); > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, original, head_target, &used, > + &rewrites_head); > + > + while ((commit = get_revision(&revs))) { > + if (commit->parents && commit->parents->next) { > + ret = error(_("replaying merge commits is not supported yet!")); > + goto out; > + } > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, commit, head_target, &used, > + &rewrites_head); > + } Merging the merge-commit check and the branch/HEAD check into a single walk in validate_revwalk() is clean. In setup_revwalk(), the revision args include --ancestry-path=<original> and ^<original>, so original is excluded from get_revision() and checking it once before the loop will not visit any branch decoration twice. All five subcommands also normalize REF_ACTION_DEFAULT to REF_ACTION_BRANCHES and call setup_revwalk() before opening an editor or modifying refs or the index, and moving reset_revision_walk() to the 'out:' label keeps object flags clean on error paths too. > out: > + reset_revision_walk(); > release_revisions(&revs); > + string_list_clear(&used, 1); > strvec_clear(&args); > + free(head_target); > return ret; > } > diff --git a/t/t3450-history.sh b/t/t3450-history.sh > index f513463b92..03bd375ef8 100755 > --- a/t/t3450-history.sh > +++ b/t/t3450-history.sh > @@ -14,4 +14,269 @@ test_expect_success 'raises an error with unknown argument' ' > test_grep "unknown subcommand: .garbage." err > ' The new tests in t/t3450-history.sh cover the worktree, bare repository, shared checkout, rebase, bisect, advice, --dry-run, --update-refs=head, and pre-editor refusal cases well. Overall this looks solid to me. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-28 0:12 ` Junio C Hamano @ 2026-11-28 7:37 ` Aniket Gupta 0 siblings, 0 replies; 16+ messages in thread From: Aniket Gupta @ 2026-11-28 7:37 UTC (permalink / raw) To: Junio C Hamano Cc: git, Patrick Steinhardt, Toon Claes, Phillip Wood, Nikita Bobko > Spelling that out directly would make the paragraph easier to follow. Makes sense. v3 should say --update-refs=head leaves the other branches alone and only refuses if the branch HEAD points to is used by another worktree. > As tested in t/t3450-history.sh, when a branch is being rebased in the > current worktree, this advice is also shown when the current worktree is > rebasing or bisecting the branch with HEAD detached, not only when it is > used by another worktree. Will fix the wording in advice.adoc. > What new things from log-tree.h are referenced by this patch? > The file seems to compile without this new inclusion. Added that by mistake. get_name_decoration() is declared in commit.h, will drop this. > Instead of enumerating every worktree with get_worktrees() just to pick > out '.is_current', we could call get_current_worktree(repo) and > free_worktree(), as wt-status.c does in wt_status_get_state(). It would > avoid reading HEAD for all linked worktrees, no? Thanks for pointing this out. Will use that. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-27 17:32 ` [PATCH v2] " Aniket Gupta 2026-11-28 0:12 ` Junio C Hamano @ 2026-11-30 11:31 ` Toon Claes 2026-11-30 12:15 ` Aniket Gupta 2026-11-30 14:39 ` Phillip Wood 2026-11-30 14:49 ` Phillip Wood 2 siblings, 2 replies; 16+ messages in thread From: Toon Claes @ 2026-11-30 11:31 UTC (permalink / raw) To: Aniket Gupta, git Cc: Patrick Steinhardt, Junio C Hamano, Phillip Wood, Nikita Bobko Aniket Gupta <aniketgupta3001@gmail.com> writes: > By default "git history" 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 the rewrite as staged changes. 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, in any worktree, > counts as used. Updating clean worktrees would be another way, but > refusing is simpler. Refuse with --dry-run as well because it will show > what a real run will do. > > The branch that the current worktree has checked out is fine, since the > command already takes care of the index and working tree of that > worktree: "drop" updates them, "reword", "split" and "squash" keep the > tree of the tip, and "fixup" takes the staged changes. A bare repository > has no such worktree, and branch_checked_out() records only one worktree > per branch, so ask the other worktrees whether they use the branch > instead of skipping it by name. --update-refs=head refuses in the same > cases. > > Refuse to rewrite HEAD while a rebase is in progress, with either > --update-refs mode, since "git rebase --abort" would throw the result > away. > > 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> > Helped-by: Phillip Wood <phillip.wood123@gmail.com> > Signed-off-by: Aniket Gupta <aniketgupta3001@gmail.com> > --- > Changes in v2: > > - Check the branch of the current worktree against the other > worktrees instead of skipping it by name. This fixes a bare > repository and a branch that two worktrees have checked out (Junio, > Phillip). > - Find the branches with the decorations of the revision walk instead > of a second walk (Phillip). > - Refuse to rewrite HEAD while a rebase is in progress (Phillip). > - Reword the log message and advice.adoc. > > Documentation/config/advice.adoc | 7 +- > Documentation/git-history.adoc | 7 + > builtin/history.c | 144 ++++++++++++++++- > t/t3450-history.sh | 265 +++++++++++++++++++++++++++++++ > 4 files changed, 413 insertions(+), 10 deletions(-) > > diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc > index 3d91c90eda..65f3ff0e02 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 to run 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..243817d28a 100644 > --- a/Documentation/git-history.adoc > +++ b/Documentation/git-history.adoc > @@ -215,6 +215,13 @@ 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. The command refuses to rewrite `HEAD` while a rebase > +is in progress in the current worktree. > > EXAMPLES > -------- > diff --git a/builtin/history.c b/builtin/history.c > index 43ba76f5a1..d0a3287c1d 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" > @@ -11,6 +12,7 @@ > #include "gettext.h" > #include "hex.h" > #include "lockfile.h" > +#include "log-tree.h" > #include "merge-ort.h" > #include "oidmap.h" > #include "parse-options.h" > @@ -27,6 +29,7 @@ > #include "tree.h" > #include "tree-walk.h" > #include "unpack-trees.h" > +#include "worktree.h" > #include "wt-status.h" > > #define GIT_HISTORY_DROP_USAGE \ > @@ -260,15 +263,100 @@ static int parse_ref_action(const struct option *opt, const char *value, int uns > return 0; > } > > -static int revwalk_contains_merges(struct repository *repo, > - const struct strvec *revwalk_args) > +/* > + * Return the path of the worktree that uses "refname", if rewriting it would > + * leave the index and working tree of that worktree behind. The branch that > + * the current worktree has checked out is fine, unless another worktree has > + * it checked out as well. > + */ > +static char *branch_used_elsewhere(struct repository *repo, > + const char *refname, > + const char *head_target) > +{ > + char *path = NULL; > + > + if (head_target && !strcmp(refname, head_target)) { > + struct worktree **worktrees = get_worktrees(repo); > + > + for (int i = 0; worktrees[i]; i++) { > + if (!worktrees[i]->is_current && > + is_shared_symref(worktrees[i], "HEAD", refname)) { By reading the comment above is_shared_symref() I've noticed this leaves a gap open. When a branch is checked out in another worktree, but has an ongoing rebase or bisect, it is not detected. You can reproduce with: test_expect_success '--update-refs=head refuses a branch that has ongoing rebase 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 && test_commit fourth && git branch other third && git worktree add ../wt -b topic && test_must_fail git -C ../wt rebase --update-refs -x false first && git checkout other && git refs list >refs-expect && test_must_fail git history drop --update-refs=head second 2>err && test_grep "^error: cannot rewrite branch .other. used by worktree at .*wt" err && git refs list >refs-actual && test_cmp refs-expect refs-actual ) ' Currently the `git history drop` command doesn't fail, and this causes the rebase to fail once completed: error: update_ref failed for ref 'refs/heads/X': cannot lock ref 'refs/heads/X': is at 602027c... but expected 8087f28... If you'd try `git branch -f X second` during the ongoing rebase, you'd see it would reject it, while `git history` doesn't. The function branch_checked_out(), used by other branches than HEAD, was changed in aa7f2fd150 (branch: consider refs under 'update-refs', 2022-07-19) to learn about this situation, but is_shared_symref() isn't. I realize this is pretty much an edge case, and I'm not saying you should address that gap in is_shared_symref() in this series, but I think it's worthwhile to mention that in the commit message. > + path = xstrdup(worktrees[i]->path); > + break; > + } > + } > + free_worktrees(worktrees); > + } else { > + const char *checked_out = branch_checked_out(refname); > + > + if (checked_out) > + path = xstrdup(checked_out); > + } > + > + return path; > +} > + > +/* "git rebase --abort" would throw away a rewrite of the detached HEAD. */ > +static bool rebase_in_progress(struct repository *repo) > +{ > + struct worktree **worktrees = get_worktrees(repo); > + struct wt_status_state state = { 0 }; > + bool ret = false; > + > + for (int i = 0; worktrees[i]; i++) { > + if (worktrees[i]->is_current) { > + wt_status_check_rebase(worktrees[i], &state); > + ret = state.rebase_in_progress || > + state.rebase_interactive_in_progress; > + break; > + } > + } > + > + wt_status_state_free_buffers(&state); > + free_worktrees(worktrees); > + return ret; > +} > + > +static void collect_used_branches(struct repository *repo, > + const struct commit *commit, > + const char *head_target, > + struct string_list *used, > + bool *rewrites_head) > { > + const struct name_decoration *decoration; > + > + for (decoration = get_name_decoration(&commit->object); > + decoration; > + decoration = decoration->next) { > + char *path; > + > + if (decoration->type == DECORATION_REF_HEAD) > + *rewrites_head = true; > + if (decoration->type != DECORATION_REF_LOCAL) > + continue; > + path = branch_used_elsewhere(repo, decoration->name, head_target); > + if (path) > + string_list_append(used, decoration->name)->util = path; > + } > +} > + > +static int validate_revwalk(struct repository *repo, > + enum ref_action action, > + struct commit *original, > + const struct strvec *revwalk_args) > +{ > + struct string_list used = STRING_LIST_INIT_DUP; > struct strvec args = STRVEC_INIT; > struct rev_info revs; > + struct commit *commit; > + bool rewrites_head = action == REF_ACTION_HEAD; > + char *head_target; > int ret; > > + head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD", > + 0, NULL, NULL); > + > strvec_pushv(&args, revwalk_args->v); > - strvec_push(&args, "--min-parents=2"); > > repo_init_revisions(repo, &revs, NULL); > > @@ -281,17 +369,59 @@ static int revwalk_contains_merges(struct repository *repo, > goto out; > } > > - if (get_revision(&revs)) { > - ret = error(_("replaying merge commits is not supported yet!")); > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, original, head_target, &used, > + &rewrites_head); > + > + while ((commit = get_revision(&revs))) { > + if (commit->parents && commit->parents->next) { > + ret = error(_("replaying merge commits is not supported yet!")); > + goto out; > + } > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, commit, head_target, &used, > + &rewrites_head); > + } > + > + if (action == REF_ACTION_HEAD && head_target && > + starts_with(head_target, "refs/heads/")) { > + char *path = branch_used_elsewhere(repo, head_target, head_target); > + if (path) > + string_list_append(&used, head_target)->util = path; > + } > + > + if (used.nr) { > + string_list_sort(&used); > + for (size_t i = 0; i < used.nr; i++) { > + const char *branch; > + > + if (!skip_prefix(used.items[i].string, "refs/heads/", &branch)) > + BUG("branch decoration without refs/heads/ prefix"); > + error(_("cannot rewrite branch '%s' used by worktree at '%s'"), > + branch, (const char *)used.items[i].util); > + } > + if (action == REF_ACTION_BRANCHES) > + advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS, > + _("Use --update-refs=head to rewrite only " > + "the current branch and leave such branches " > + "untouched.")); There are situations where using '--update-refs=head' would not help. For example when a branch is checked out in another worktree (using --force), or when you're operating on a bare repository. > + ret = -1; > + goto out; > + } > + > + if (rewrites_head && rebase_in_progress(repo)) { > + ret = error(_("cannot rewrite HEAD while a rebase is in progress")); > goto out; > } > > - reset_revision_walk(); > ret = 0; > > out: > + reset_revision_walk(); > release_revisions(&revs); > + string_list_clear(&used, 1); > strvec_clear(&args); > + free(head_target); > return ret; > } > > @@ -354,7 +484,7 @@ static int setup_revwalk(struct repository *repo, > strvec_push(&args, "HEAD"); > } > > - ret = revwalk_contains_merges(repo, &args); > + ret = validate_revwalk(repo, action, original, &args); > if (ret < 0) > goto out; -- Laters, Toon ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-30 11:31 ` Toon Claes @ 2026-11-30 12:15 ` Aniket Gupta 2026-11-30 14:39 ` Phillip Wood 1 sibling, 0 replies; 16+ messages in thread From: Aniket Gupta @ 2026-11-30 12:15 UTC (permalink / raw) To: Toon Claes, git Cc: Patrick Steinhardt, Junio C Hamano, Phillip Wood, Nikita Bobko > By reading the comment above is_shared_symref() I've noticed this > leaves a gap open. When a branch is checked out in another worktree, > but has an ongoing rebase or bisect, it is not detected. Thanks. I indeed can repro this. "git history drop --update-refs=head second" exits 0 and moves 'other', and the rebase in wt then fails with the "cannot lock ref" error which you showed. A rebase or bisect of the branch itself is detected. I checked both. The gap is a branch that another worktree's "rebase --update-refs" is going to update. The default mode misses it as well when the other worktree rebases a detached HEAD. > I realize this is pretty much an edge case, and I'm not saying you > should address that gap in is_shared_symref() in this series, but I > think it's worthwhile to mention that in the commit message. I think I can close it in builtin/history.c by also looking at the update-refs state of the other worktrees, so I will try that in v3 with your test. If it turns out to be bigger than that, I will mention it in the commit message as you suggested. > There are situations where using '--update-refs=head' would not help. > For example when a branch is checked out in another worktree (using > --force), or when you're operating on a bare repository. True. Can repro both when the branch is the one HEAD points to. It also happens when the current worktree is in the middle of a rebase. In v3, I will not show the hint when --update-refs=head would refuse too. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-30 11:31 ` Toon Claes 2026-11-30 12:15 ` Aniket Gupta @ 2026-11-30 14:39 ` Phillip Wood 2026-11-30 17:13 ` Aniket Gupta 1 sibling, 1 reply; 16+ messages in thread From: Phillip Wood @ 2026-11-30 14:39 UTC (permalink / raw) To: Toon Claes, Aniket Gupta, git Cc: Patrick Steinhardt, Junio C Hamano, Nikita Bobko On 30/11/2026 11:31, Toon Claes wrote: > Aniket Gupta <aniketgupta3001@gmail.com> writes: > >> +static char *branch_used_elsewhere(struct repository *repo, >> + const char *refname, >> + const char *head_target) >> +{ >> + char *path = NULL; >> + >> + if (head_target && !strcmp(refname, head_target)) { >> + struct worktree **worktrees = get_worktrees(repo); >> + >> + for (int i = 0; worktrees[i]; i++) { >> + if (!worktrees[i]->is_current && >> + is_shared_symref(worktrees[i], "HEAD", refname)) { > > By reading the comment above is_shared_symref() I've noticed this leaves > a gap open. When a branch is checked out in another worktree, but has an > ongoing rebase or bisect, it is not detected. Good catch. I wonder if branch_used_elsewhere() would better in branch.c and based around register_checked_out_branch(). For example "git checkout" calls branch_checked_out() but I think it really wants to know if a branch is in use elsewhere. Thanks Phillip > >> + path = xstrdup(worktrees[i]->path); >> + break; >> + } >> + } >> + free_worktrees(worktrees); >> + } else { >> + const char *checked_out = branch_checked_out(refname); >> + >> + if (checked_out) >> + path = xstrdup(checked_out); >> + } >> + >> + return path; >> +} >> + >> +/* "git rebase --abort" would throw away a rewrite of the detached HEAD. */ >> +static bool rebase_in_progress(struct repository *repo) >> +{ >> + struct worktree **worktrees = get_worktrees(repo); >> + struct wt_status_state state = { 0 }; >> + bool ret = false; >> + >> + for (int i = 0; worktrees[i]; i++) { >> + if (worktrees[i]->is_current) { >> + wt_status_check_rebase(worktrees[i], &state); >> + ret = state.rebase_in_progress || >> + state.rebase_interactive_in_progress; >> + break; >> + } >> + } >> + >> + wt_status_state_free_buffers(&state); >> + free_worktrees(worktrees); >> + return ret; >> +} >> + >> +static void collect_used_branches(struct repository *repo, >> + const struct commit *commit, >> + const char *head_target, >> + struct string_list *used, >> + bool *rewrites_head) >> { >> + const struct name_decoration *decoration; >> + >> + for (decoration = get_name_decoration(&commit->object); >> + decoration; >> + decoration = decoration->next) { >> + char *path; >> + >> + if (decoration->type == DECORATION_REF_HEAD) >> + *rewrites_head = true; >> + if (decoration->type != DECORATION_REF_LOCAL) >> + continue; >> + path = branch_used_elsewhere(repo, decoration->name, head_target); >> + if (path) >> + string_list_append(used, decoration->name)->util = path; >> + } >> +} >> + >> +static int validate_revwalk(struct repository *repo, >> + enum ref_action action, >> + struct commit *original, >> + const struct strvec *revwalk_args) >> +{ >> + struct string_list used = STRING_LIST_INIT_DUP; >> struct strvec args = STRVEC_INIT; >> struct rev_info revs; >> + struct commit *commit; >> + bool rewrites_head = action == REF_ACTION_HEAD; >> + char *head_target; >> int ret; >> >> + head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD", >> + 0, NULL, NULL); >> + >> strvec_pushv(&args, revwalk_args->v); >> - strvec_push(&args, "--min-parents=2"); >> >> repo_init_revisions(repo, &revs, NULL); >> >> @@ -281,17 +369,59 @@ static int revwalk_contains_merges(struct repository *repo, >> goto out; >> } >> >> - if (get_revision(&revs)) { >> - ret = error(_("replaying merge commits is not supported yet!")); >> + if (action == REF_ACTION_BRANCHES) >> + collect_used_branches(repo, original, head_target, &used, >> + &rewrites_head); >> + >> + while ((commit = get_revision(&revs))) { >> + if (commit->parents && commit->parents->next) { >> + ret = error(_("replaying merge commits is not supported yet!")); >> + goto out; >> + } >> + if (action == REF_ACTION_BRANCHES) >> + collect_used_branches(repo, commit, head_target, &used, >> + &rewrites_head); >> + } >> + >> + if (action == REF_ACTION_HEAD && head_target && >> + starts_with(head_target, "refs/heads/")) { >> + char *path = branch_used_elsewhere(repo, head_target, head_target); >> + if (path) >> + string_list_append(&used, head_target)->util = path; >> + } >> + >> + if (used.nr) { >> + string_list_sort(&used); >> + for (size_t i = 0; i < used.nr; i++) { >> + const char *branch; >> + >> + if (!skip_prefix(used.items[i].string, "refs/heads/", &branch)) >> + BUG("branch decoration without refs/heads/ prefix"); >> + error(_("cannot rewrite branch '%s' used by worktree at '%s'"), >> + branch, (const char *)used.items[i].util); >> + } >> + if (action == REF_ACTION_BRANCHES) >> + advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS, >> + _("Use --update-refs=head to rewrite only " >> + "the current branch and leave such branches " >> + "untouched.")); > > There are situations where using '--update-refs=head' would not help. > For example when a branch is checked out in another worktree (using > --force), or when you're operating on a bare repository. > >> + ret = -1; >> + goto out; >> + } >> + >> + if (rewrites_head && rebase_in_progress(repo)) { >> + ret = error(_("cannot rewrite HEAD while a rebase is in progress")); >> goto out; >> } >> >> - reset_revision_walk(); >> ret = 0; >> >> out: >> + reset_revision_walk(); >> release_revisions(&revs); >> + string_list_clear(&used, 1); >> strvec_clear(&args); >> + free(head_target); >> return ret; >> } >> >> @@ -354,7 +484,7 @@ static int setup_revwalk(struct repository *repo, >> strvec_push(&args, "HEAD"); >> } >> >> - ret = revwalk_contains_merges(repo, &args); >> + ret = validate_revwalk(repo, action, original, &args); >> if (ret < 0) >> goto out; > ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-30 14:39 ` Phillip Wood @ 2026-11-30 17:13 ` Aniket Gupta 0 siblings, 0 replies; 16+ messages in thread From: Aniket Gupta @ 2026-11-30 17:13 UTC (permalink / raw) To: phillip.wood Cc: Toon Claes, Patrick Steinhardt, Junio C Hamano, Nikita Bobko, git > I wonder if branch_used_elsewhere() would better in branch.c and based > around register_checked_out_branch(). Good idea. I see that branch.c already tracks checkout, rebase, bisect and --update-refs, so the gap Toon found can be closed without special cases in history.c. Will do in v3. > For example "git checkout" calls branch_checked_out() but I think it > really wants to know if a branch is in use elsewhere. Checkout goes through die_if_checked_out(), which does not see branches that another worktree's rebase --update-refs is going to update. How about keeping checkout out of this patch and looking at it afterwards? ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-27 17:32 ` [PATCH v2] " Aniket Gupta 2026-11-28 0:12 ` Junio C Hamano 2026-11-30 11:31 ` Toon Claes @ 2026-11-30 14:49 ` Phillip Wood 2026-11-30 17:13 ` Aniket Gupta 2 siblings, 1 reply; 16+ messages in thread From: Phillip Wood @ 2026-11-30 14:49 UTC (permalink / raw) To: Aniket Gupta, git Cc: Patrick Steinhardt, Toon Claes, Junio C Hamano, Nikita Bobko Hi Aniket On 27/11/2026 17:32, Aniket Gupta wrote: > diff --git a/Documentation/config/advice.adoc b/Documentation/config/advice.adoc > index 3d91c90eda..65f3ff0e02 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 to run because a local branch is > + used by another worktree, or, for `squash`, because it cannot be > + rewritten as a descendant of the squashed commit, I'd finish the sentence here. > to tell the user about `--update-refs=head`. This ends up being confusing when it is tacked on to the end of the sentance explaining when the advice is shown. We don't describe the advice in the documentation of the other options so I'd drop it. > 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..243817d28a 100644 > --- a/Documentation/git-history.adoc > +++ b/Documentation/git-history.adoc > @@ -215,6 +215,13 @@ 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. That's true for other worktrees, but not the current worktree where the reason is that we'll end up losing work. I'd split it up A branch that is used by another worktree cannot be rewritten because the index and working tree of that worktree would not match the rewritten branch. A branch that is being rebased in the current worktree cannot be rewritten because the rewrite cause the rebase to fail. A branch that is being bisected in the current worktree cannot be rewritten because ... (is there actually a good reason?) > With > +`branches` the command refuses in that case, so use `head` to rewrite only > +the current branch. The command refuses to rewrite `HEAD` while a rebase > +is in progress in the current worktree. This is looking good, I don't have anything else to add to what Junio and Toon have said. Thanks Phillip > EXAMPLES > -------- > diff --git a/builtin/history.c b/builtin/history.c > index 43ba76f5a1..d0a3287c1d 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" > @@ -11,6 +12,7 @@ > #include "gettext.h" > #include "hex.h" > #include "lockfile.h" > +#include "log-tree.h" > #include "merge-ort.h" > #include "oidmap.h" > #include "parse-options.h" > @@ -27,6 +29,7 @@ > #include "tree.h" > #include "tree-walk.h" > #include "unpack-trees.h" > +#include "worktree.h" > #include "wt-status.h" > > #define GIT_HISTORY_DROP_USAGE \ > @@ -260,15 +263,100 @@ static int parse_ref_action(const struct option *opt, const char *value, int uns > return 0; > } > > -static int revwalk_contains_merges(struct repository *repo, > - const struct strvec *revwalk_args) > +/* > + * Return the path of the worktree that uses "refname", if rewriting it would > + * leave the index and working tree of that worktree behind. The branch that > + * the current worktree has checked out is fine, unless another worktree has > + * it checked out as well. > + */ > +static char *branch_used_elsewhere(struct repository *repo, > + const char *refname, > + const char *head_target) > +{ > + char *path = NULL; > + > + if (head_target && !strcmp(refname, head_target)) { > + struct worktree **worktrees = get_worktrees(repo); > + > + for (int i = 0; worktrees[i]; i++) { > + if (!worktrees[i]->is_current && > + is_shared_symref(worktrees[i], "HEAD", refname)) { > + path = xstrdup(worktrees[i]->path); > + break; > + } > + } > + free_worktrees(worktrees); > + } else { > + const char *checked_out = branch_checked_out(refname); > + > + if (checked_out) > + path = xstrdup(checked_out); > + } > + > + return path; > +} > + > +/* "git rebase --abort" would throw away a rewrite of the detached HEAD. */ > +static bool rebase_in_progress(struct repository *repo) > +{ > + struct worktree **worktrees = get_worktrees(repo); > + struct wt_status_state state = { 0 }; > + bool ret = false; > + > + for (int i = 0; worktrees[i]; i++) { > + if (worktrees[i]->is_current) { > + wt_status_check_rebase(worktrees[i], &state); > + ret = state.rebase_in_progress || > + state.rebase_interactive_in_progress; > + break; > + } > + } > + > + wt_status_state_free_buffers(&state); > + free_worktrees(worktrees); > + return ret; > +} > + > +static void collect_used_branches(struct repository *repo, > + const struct commit *commit, > + const char *head_target, > + struct string_list *used, > + bool *rewrites_head) > { > + const struct name_decoration *decoration; > + > + for (decoration = get_name_decoration(&commit->object); > + decoration; > + decoration = decoration->next) { > + char *path; > + > + if (decoration->type == DECORATION_REF_HEAD) > + *rewrites_head = true; > + if (decoration->type != DECORATION_REF_LOCAL) > + continue; > + path = branch_used_elsewhere(repo, decoration->name, head_target); > + if (path) > + string_list_append(used, decoration->name)->util = path; > + } > +} > + > +static int validate_revwalk(struct repository *repo, > + enum ref_action action, > + struct commit *original, > + const struct strvec *revwalk_args) > +{ > + struct string_list used = STRING_LIST_INIT_DUP; > struct strvec args = STRVEC_INIT; > struct rev_info revs; > + struct commit *commit; > + bool rewrites_head = action == REF_ACTION_HEAD; > + char *head_target; > int ret; > > + head_target = refs_resolve_refdup(get_main_ref_store(repo), "HEAD", > + 0, NULL, NULL); > + > strvec_pushv(&args, revwalk_args->v); > - strvec_push(&args, "--min-parents=2"); > > repo_init_revisions(repo, &revs, NULL); > > @@ -281,17 +369,59 @@ static int revwalk_contains_merges(struct repository *repo, > goto out; > } > > - if (get_revision(&revs)) { > - ret = error(_("replaying merge commits is not supported yet!")); > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, original, head_target, &used, > + &rewrites_head); > + > + while ((commit = get_revision(&revs))) { > + if (commit->parents && commit->parents->next) { > + ret = error(_("replaying merge commits is not supported yet!")); > + goto out; > + } > + if (action == REF_ACTION_BRANCHES) > + collect_used_branches(repo, commit, head_target, &used, > + &rewrites_head); > + } > + > + if (action == REF_ACTION_HEAD && head_target && > + starts_with(head_target, "refs/heads/")) { > + char *path = branch_used_elsewhere(repo, head_target, head_target); > + if (path) > + string_list_append(&used, head_target)->util = path; > + } > + > + if (used.nr) { > + string_list_sort(&used); > + for (size_t i = 0; i < used.nr; i++) { > + const char *branch; > + > + if (!skip_prefix(used.items[i].string, "refs/heads/", &branch)) > + BUG("branch decoration without refs/heads/ prefix"); > + error(_("cannot rewrite branch '%s' used by worktree at '%s'"), > + branch, (const char *)used.items[i].util); > + } > + if (action == REF_ACTION_BRANCHES) > + advise_if_enabled(ADVICE_HISTORY_UPDATE_REFS, > + _("Use --update-refs=head to rewrite only " > + "the current branch and leave such branches " > + "untouched.")); > + ret = -1; > + goto out; > + } > + > + if (rewrites_head && rebase_in_progress(repo)) { > + ret = error(_("cannot rewrite HEAD while a rebase is in progress")); > goto out; > } > > - reset_revision_walk(); > ret = 0; > > out: > + reset_revision_walk(); > release_revisions(&revs); > + string_list_clear(&used, 1); > strvec_clear(&args); > + free(head_target); > return ret; > } > > @@ -354,7 +484,7 @@ static int setup_revwalk(struct repository *repo, > strvec_push(&args, "HEAD"); > } > > - ret = revwalk_contains_merges(repo, &args); > + ret = validate_revwalk(repo, action, original, &args); > if (ret < 0) > goto out; > > diff --git a/t/t3450-history.sh b/t/t3450-history.sh > index f513463b92..03bd375ef8 100755 > --- a/t/t3450-history.sh > +++ b/t/t3450-history.sh > @@ -14,4 +14,269 @@ 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 && > + 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 && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual > + ) > +' > + > +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_expect_success 'refuses to rewrite HEAD while a rebase is in progress' ' > + test_when_finished "rm -rf repo" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit base && > + git checkout -b topic && > + test_commit topic-one && > + test_commit topic-two && > + git checkout main && > + test_commit main-new && > + git checkout topic && > + git rev-parse topic >orig && > + test_must_fail git rebase -x false main && > + git rev-parse HEAD >head-expect && > + > + test_must_fail git history drop HEAD~1 2>err && > + test_grep "^error: cannot rewrite HEAD while a rebase is in progress" err && > + test_must_fail git history drop --update-refs=head HEAD 2>err && > + test_grep "^error: cannot rewrite HEAD while a rebase is in progress" err && > + git rev-parse HEAD >head-actual && > + test_cmp head-expect head-actual && > + > + git rebase --abort && > + git rev-parse topic >actual && > + test_cmp orig actual > + ) > +' > + > +test_expect_success 'rewrites commits unrelated to HEAD while a rebase is in progress' ' > + test_when_finished "rm -rf repo" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit base && > + git checkout -b other && > + test_commit other-one && > + test_commit other-two && > + git checkout -b topic base && > + test_commit topic-one && > + git checkout main && > + test_commit main-new && > + git checkout topic && > + test_must_fail git rebase -x false main && > + git rev-parse HEAD >head-expect && > + > + git history drop other-one && > + git log --format=%s other >actual && > + test_write_lines other-two base >expect && > + test_cmp expect actual && > + git rev-parse HEAD >head-actual && > + test_cmp head-expect head-actual && > + git rebase --abort > + ) > +' > + > +test_expect_success 'refuses to rewrite the branch a bare repository names in HEAD when a worktree uses it' ' > + test_when_finished "rm -rf repo repo.git wt" && > + git init repo --initial-branch=main && > + test_commit -C repo first && > + test_commit -C repo second && > + test_commit -C repo third && > + git clone --bare repo repo.git && > + git -C repo.git worktree add ../wt main && > + > + git -C repo.git refs list >refs-expect && > + test_must_fail git -C repo.git history drop second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && > + test_must_fail git -C repo.git history drop --update-refs=head second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && > + git -C repo.git refs list >refs-actual && > + test_cmp refs-expect refs-actual > +' > + > +test_expect_success 'refuses to rewrite a branch that two worktrees have checked out' ' > + 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 --force ../wt main && > + > + 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 .*wt" err && > + test_must_fail git -C ../wt history drop second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*repo" err && > + test_must_fail git history drop --update-refs=head second 2>err && > + test_grep "^error: cannot rewrite branch .main. used by worktree at .*wt" err && > + test_grep ! "^hint:" err && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual > + ) > +' > + > +test_expect_success 'refuses to rewrite the commit that a branch of another worktree points at' ' > + 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 second && > + > + git refs list >refs-expect && > + test_must_fail git history reword second 2>err && > + test_grep "^error: cannot rewrite branch .other. used by worktree at .*wt" err && > + test_grep ! "branch .main." err && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual > + ) > +' > + > +test_expect_success 'every subcommand refuses before it does anything' ' > + test_when_finished "rm -rf repo wt" && > + git init repo --initial-branch=main && > + ( > + cd repo && > + test_commit first && > + test_commit second && > + test_commit third && > + test_commit fourth && > + git worktree add ../wt -b other && > + echo changed >first.t && > + git add first.t && > + write_script fail-editor <<-\EOF && > + >editor-ran > + exit 1 > + EOF > + test_set_editor "$(pwd)/fail-editor" && > + > + git refs list >refs-expect && > + git diff --cached >index-expect && > + for cmd in "drop second" "reword second" "split second" \ > + "fixup second" "squash first..third" > + do > + test_must_fail git history $cmd </dev/null 2>err && > + test_grep "^error: cannot rewrite branch .other. used by worktree at" err && > + test_path_is_missing editor-ran && > + git refs list >refs-actual && > + test_cmp refs-expect refs-actual && > + git diff --cached >index-actual && > + test_cmp index-expect index-actual || return 1 > + done > + ) > +' > + > test_done ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: [PATCH v2] history: refuse to rewrite branches used by other worktrees 2026-11-30 14:49 ` Phillip Wood @ 2026-11-30 17:13 ` Aniket Gupta 0 siblings, 0 replies; 16+ messages in thread From: Aniket Gupta @ 2026-11-30 17:13 UTC (permalink / raw) To: phillip.wood Cc: Toon Claes, Patrick Steinhardt, Junio C Hamano, Nikita Bobko, git > I'd finish the sentence here. > [...] > We don't describe the advice in the documentation of the other > options so I'd drop it. Will be done in v3. > That's true for other worktrees, but not the current worktree where > the reason is that we'll end up losing work. I'd split it up We can split it up. A worktree that has the branch checked out can be left with an index and working tree that no longer match the branch. A rebase is a different problem which is the same in every worktree, i.e. if the branch is rewritten during the rebase, "git rebase --continue" either fails, or throws the rewrite away, and "git rebase --abort" brings back the old branch. In the docs I will group the reasons by what is happening to the branch, not by which worktree it is in. > A branch that is being bisected in the current worktree cannot be > rewritten because ... (is there actually a good reason?) I rewrote the branch by hand while it was being bisected in the current worktree and in a different one. Bisect could continue and "git bisect reset" checked out the rewritten branch without problems. One thing I noticed is that the result points to a commit from the old history, which is no longer on the branch. I included this check because "git branch -f" and "git rebase --update-refs" also leave a branch alone while it is being bisected. I am in favour of keeping it. Suggestions? ^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2026-11-30 17:13 UTC | newest] Thread overview: 16+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-23 10:39 [PATCH] history: refuse to rewrite branches used by other worktrees Aniket Gupta 2026-11-23 13:10 ` Phillip Wood 2026-11-23 21:54 ` Aniket Gupta 2026-11-24 10:49 ` Phillip Wood 2026-11-24 14:09 ` Aniket Gupta 2026-11-23 20:14 ` Junio C Hamano 2026-11-23 21:54 ` Aniket Gupta 2026-11-27 17:32 ` [PATCH v2] " Aniket Gupta 2026-11-28 0:12 ` Junio C Hamano 2026-11-28 7:37 ` Aniket Gupta 2026-11-30 11:31 ` Toon Claes 2026-11-30 12:15 ` Aniket Gupta 2026-11-30 14:39 ` Phillip Wood 2026-11-30 17:13 ` Aniket Gupta 2026-11-30 14:49 ` Phillip Wood 2026-11-30 17:13 ` Aniket Gupta
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox