Rename a branch/worktree folder
Sometimes I want to change the name of the branch and corresponding worktree folder. I can do it manually, but I reckon this is something that wt could streamline.
Thanks for the suggestion! I searched the codebase and didn't find an existing command for renaming a branch and its worktree folder together.
The closest related functionality is wt step relocate, which moves worktree directories to match the configured worktree-path template — but it operates on the folder path only and doesn't rename branches.
A rename command would need to coordinate git branch -m with git worktree move (and handle edge cases like the main worktree, which can't use git worktree move). Some of the relocation infrastructure in the codebase could potentially be reused for this.
I'll leave it for a maintainer to evaluate and prioritize.
@worktrunk-bot can you show us what it would look like?
Here's a sketch of what wt rename could look like:
Usage
# Rename branch + move worktree folder to match
wt rename old-branch new-branch
# Preview without changing anything
wt rename old-branch new-branch --dry-run
What it would do
- Rename the branch —
git branch -m old-branch new-branch - Move the worktree folder —
git worktree move <old-path> <new-path>(new path computed from theworktree-pathtemplate, same aswt switchuses) - Update shell cd — if you're inside the renamed worktree, the shell wrapper would cd you into the new location
Example session
$ wt list
Branch Path
@ feature ~/project.feature
+ main ~/project
$ wt rename feature billing-api
$ wt list
Branch Path
@ billing-api ~/project.billing-api
+ main ~/project
Edge cases to handle
- Main worktree — can't use
git worktree move; would need the same special handling aswt step relocate(switch to default branch + create new linked worktree). Might be simplest to just reject this case initially. - Dirty worktree — refuse by default, or offer
--committo auto-commit first (matchingrelocate's pattern) - Upstream tracking —
git branch -mupdates the local branch but doesn't rename the remote branch. Could print a hint: "Remote branch unchanged — push the new branch and delete the old one withgit push origin new-branch :old-branch" - Path already occupied — another worktree or regular directory sits at the computed path. Could offer
--clobberlikerelocatedoes.
Placement
This feels like a wt step rename (alongside relocate, prune, promote) rather than a top-level command — it's a power-user operation that coordinates two git primitives. But it could go either way.
That's roughly the shape of it. Implementation-wise it would reuse the same path-computation and worktree-move infrastructure that step relocate already has.
why not just wt switch -c --base=$old?
Good point — wt switch -c new-branch --base=old-branch + wt remove old-branch covers the core use case with existing commands. The main difference is that a dedicated rename would be a single atomic step (no intermediate state with two worktrees), but that's a pretty thin justification for a new command.
Probably not worth adding unless this comes up more.
will close but feel free to reopen with more thoughts. thanks @craigdallimore
Thanks, I had not discovered the existing approach. Agree this is superfluous
I find myself quite often kicking of an agent session on a worktree for one reason. Over time it becomes more concrete or pivots. I also do not use wt for trunk based development but on a project with feature branches, where the branch name should make sense (including the issue number).
For my usage wt rename <old> <new> would be very convenient and I feel it is in line with the ergonomics of other commands.
For my usage
wt rename <old> <new>would be very convenient and I feel it is in line with the ergonomics of other commands.
would this rename the branch too?
I would encourage using an alias for this; @worktrunk-bot can you write & test some ideas — what would a rename alias look like which
- took a
base&branchvar - committed that worktree if it exists
- ran
wt switchwith the base, creating the new branch
ensure you test so it's accurate
@worktrunk-bot try again please
Here's a tested rename alias. Add to your user config (~/.config/worktrunk/config.toml):
[aliases]
rename = """
set -e
# Commit any uncommitted changes on the base worktree
wt step commit --branch {{ base }} || true
# Create the new branch + worktree from the base
wt switch --create {{ branch }} --base {{ base }} --yes
"""
Usage:
wt step rename --base=feature-123 --branch=billing-api
wt remove feature-123 # clean up old worktree
What it does:
- Runs
wt step commitagainst the base worktree (stages everything and generates a commit message via LLM, falling back to a default when no LLM is configured). The|| truemakes it a no-op when the base is clean or has no worktree. - Creates a new branch + worktree from the base via
wt switch --create.
Tested scenarios
-
Dirty base worktree — auto-commits changes, then creates the new branch:
$ wt step rename --base=feature-123 --branch=billing-api ▲ Auto-staging 1 untracked path: new-file.txt ◎ Committing changes with default message... (1 file, +1) ↳ Using fallback commit message. For LLM setup guide, run wt config --help Changes to new-file.txt ✓ Committed changes @ ae552ef ✓ Created branch billing-api from feature-123 and worktree @ ../project.billing-api -
Clean base worktree —
wt step commitreportsNothing to commit, the|| trueswallows it, then the new branch is created. -
Base has no worktree (branch-only) —
wt step commitreportsno worktree for branch '<base>', the|| trueswallows it, then the new branch is created.
See aliases docs for more on template variables and --KEY=VALUE bindings.
@worktrunk-bot nice. can you put it onto multiple lines though; it's difficult to read atm. edit your comment above
Came here looking for a wt rename command. It's nice that there are alternatives here, but they're a bit advanced and hard to remember.
My use case is that VS Code's Agents application creates a worktree for me with a folder and branch name that often doesn't map to how I think about the purpose of the work there, so I want to rename it to give it a clearer focus. I'll explore the options here, but they weren't obvious.
check out wt step relocate to just get everything onto the expected paths...
@worktrunk-bot can you edit the comment above again please — use wt step commit rather than the custom commit func there