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

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

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Nov 23, 2026, 13:10 UTC
Message-ID
<a0ebd98f-669e-4ff2-bca2-cfd5da5a7316@gmail.com>
In-Reply-To
<20261123103911.98860-1-aniketgupta3001@gmail.com>
Hi Aniket
Thanks for working on this, it is a useful improvement.
On 23/11/2026 10:39, Aniket Gupta wrote:
> By default "git history" also rewrites every local branch that descends
"also" is unnecessary here
> from the commit it changes. If another worktree has such a branch
> checked out, the branch moves but the index and working tree of that
> worktree stay behind. So it shows staged changes that nobody made.
The staged changes it shows were made by "git history" - they did not appear from nowhere. An alternative to refusing to run would be to update those worktrees if they are clean, but failing is the simpler option to implement.
Show 9 quoted lines
> 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.
Show 12 quoted lines
> > 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 ...
Show 19 quoted lines
> +		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.
Show 21 quoted lines
> +/*
> + * 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
Show 185 quoted lines
> +		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
Previous: Aniket GuptaNext: Aniket Gupta
Message 2 of 7 in “history: refuse to rewrite branches used by other worktrees”
  1. history: refuse to rewrite branches used by other worktreesAniket Gupta, Nov 23, 2026
  2. Phillip WoodNov 23, 2026
  3. Aniket GuptaNov 23, 2026
  4. Phillip WoodNov 24, 2026
  5. Aniket GuptaNov 24, 2026
  6. Junio C HamanoNov 23, 2026
  7. Aniket GuptaNov 23, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.