Re: [PATCH v2] worktree list: add --branch to select worktrees by branch
On Sat, Oct 24, 2026, at 18:53, Jasper Mayone wrote:
> There is no direct way to ask which worktree has a given branch
> checked out. "git worktree list --porcelain" prints the branch of
> every worktree, so a script has to parse the whole list and match the
> "branch" lines itself.
git-for-each-ref(1) (and git-branch(1) etc.) have `worktreepath`.
git worktree add ../test-branch
git worktree remove $(git for-each-ref \
--format='%(worktreepath)' refs/heads/test-branch)
git branch -d test-branchIt will not list the worktree path if that branch is being rebased.
Show 31 quoted lines
>
> Teach "git worktree list" a "--branch=<branch>" option that lists
> only the worktrees that have <branch> checked out. It can be given
> more than once. A worktree that is rebasing or bisecting a branch
> counts as having it checked out, as it does for "git branch -d" and
> "git worktree add". <branch> is a branch name, or a full name under
> refs/heads/.
>
> This keeps the choice of branches in "git branch", which already has
> --merged, --contains, --points-at and patterns. For example, to list
> the worktrees whose branch is merged into origin/main:
>
> git worktree list \
> $(git branch --format='--branch=%(refname)' --merged origin/main)
>
> The --format is needed because the default output of "git branch"
> marks the current branch with "*" and the branches checked out in
> other worktrees with "+". It uses the full name because
> %(refname:short) gives "heads/topic" when a tag is also called
> "topic".
>
> Each worktree's branch is found once and looked up in a set of the
> requested branches, so feeding it every branch of a large repository
> stays cheap.
>
> A detached worktree has no branch, so it is never listed.
>
> Suggested-by: Junio C Hamano <gitster@pobox.com>
> Signed-off-by: Jasper Mayone <me@jaspermayone.com>
> ---
>[snip]