worktrunk icon indicating copy to clipboard operation
worktrunk copied to clipboard

Rename a branch/worktree folder

Open craigdallimore opened this issue 8 months ago • 15 comments

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.

craigdallimore avatar Mar 11 '26 21:03 craigdallimore

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 avatar Mar 11 '26 21:03 worktrunk-bot

@worktrunk-bot can you show us what it would look like?

craigdallimore avatar Mar 11 '26 21:03 craigdallimore

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

  1. Rename the branch — git branch -m old-branch new-branch
  2. Move the worktree folder — git worktree move <old-path> <new-path> (new path computed from the worktree-path template, same as wt switch uses)
  3. 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 as wt 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 --commit to auto-commit first (matching relocate's pattern)
  • Upstream tracking — git branch -m updates 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 with git push origin new-branch :old-branch"
  • Path already occupied — another worktree or regular directory sits at the computed path. Could offer --clobber like relocate does.

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.

worktrunk-bot avatar Mar 11 '26 21:03 worktrunk-bot

why not just wt switch -c --base=$old?

max-sixty avatar Mar 11 '26 21:03 max-sixty

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.

worktrunk-bot avatar Mar 11 '26 21:03 worktrunk-bot

will close but feel free to reopen with more thoughts. thanks @craigdallimore

max-sixty avatar Mar 11 '26 21:03 max-sixty

Thanks, I had not discovered the existing approach. Agree this is superfluous

craigdallimore avatar Mar 11 '26 22:03 craigdallimore

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.

benjaminbauer avatar Apr 08 '26 16:04 benjaminbauer

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 & branch var
  • committed that worktree if it exists
  • ran wt switch with the base, creating the new branch

ensure you test so it's accurate

max-sixty avatar Apr 08 '26 17:04 max-sixty

@worktrunk-bot try again please

max-sixty avatar Apr 08 '26 18:04 max-sixty

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:

  1. Runs wt step commit against the base worktree (stages everything and generates a commit message via LLM, falling back to a default when no LLM is configured). The || true makes it a no-op when the base is clean or has no worktree.
  2. 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 commit reports Nothing to commit, the || true swallows it, then the new branch is created.

  • Base has no worktree (branch-only) — wt step commit reports no worktree for branch '<base>', the || true swallows it, then the new branch is created.

See aliases docs for more on template variables and --KEY=VALUE bindings.

worktrunk-bot avatar Apr 08 '26 18:04 worktrunk-bot

@worktrunk-bot nice. can you put it onto multiple lines though; it's difficult to read atm. edit your comment above

max-sixty avatar Apr 08 '26 19:04 max-sixty

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.

drewnoakes avatar Apr 24 '26 04:04 drewnoakes

check out wt step relocate to just get everything onto the expected paths...

max-sixty avatar Apr 24 '26 05:04 max-sixty

@worktrunk-bot can you edit the comment above again please — use wt step commit rather than the custom commit func there

max-sixty avatar Apr 24 '26 05:04 max-sixty