Skip to content

feat: add --branch flag to wt step commit#1750

Merged
max-sixty merged 3 commits intomainfrom
step-commit-branch-flag
Mar 30, 2026
Merged

feat: add --branch flag to wt step commit#1750
max-sixty merged 3 commits intomainfrom
step-commit-branch-flag

Conversation

@worktrunk-bot
Copy link
Copy Markdown
Collaborator

Summary

  • Adds --branch / -b flag to wt step commit, allowing commits in a different worktree without cd-ing there: wt step commit -b feature-x
  • Adds CommandEnv::for_branch() constructor that resolves a worktree path from a branch name
  • Updates CommitOptions::commit() to use the context's worktree_path for git operations (staging, status, commit) instead of hardcoding current_worktree() — this makes it work correctly for both the default (cwd) and --branch cases

This is the first step toward #1637. The --branch flag can later be added to other step subcommands and eventually to wt merge.

Test plan

  • test_step_commit_branch_flag — commits to a feature worktree while cwd is the main worktree, verifies the commit lands in the correct worktree
  • test_step_commit_branch_flag_nonexistent — errors cleanly when the branch has no worktree
  • All 1261 integration tests pass
  • Help snapshots and doc sync updated automatically

🤖 Generated with Claude Code

Allows committing in a different worktree without changing directories:
`wt step commit --branch feature-x`

Adds `CommandEnv::for_branch()` which resolves the worktree path from a
branch name. Updates `CommitOptions::commit()` to use the context's
worktree path for all git operations instead of defaulting to cwd.

Closes #1637

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@max-sixty
Copy link
Copy Markdown
Owner

what happens on a detached worktree?

@worktrunk-bot
Copy link
Copy Markdown
Collaborator Author

A detached worktree can't be targeted with --branch — you get "no worktree for branch 'X'". The lookup in worktree_for_branch() matches on the branch field, which is None for detached worktrees, so it never finds a match.

This is consistent with the project's model of addressing worktrees by branch name. If you're in a detached worktree, wt step commit (without --branch) still works fine via cwd — you just can't target one remotely.

Should I add an explicit note about this in the help text, or is the current error message clear enough?

@max-sixty
Copy link
Copy Markdown
Owner

This is consistent with the project's model of addressing worktrees by branch name. If you're in a detached worktree, wt step commit (without --branch) still works fine via cwd — you just can't target one remotely.

ok great — would you mind confirming?

@worktrunk-bot
Copy link
Copy Markdown
Collaborator Author

I confirmed — my earlier claim was wrong. wt step commit (without --branch) does not work in a detached worktree. It fails with:

✗ Cannot commit: not on a branch (detached HEAD)
↳ To switch to a branch, run git switch <branch>

This is because CommandEnv::for_action() calls require_current_branch(), which errors on detached HEAD.

So the full picture for detached worktrees:

  • --branch <name>: Can't target a detached worktree (no branch to look up) — errors with "no worktree for branch 'X'"
  • No --branch (cwd): Also fails if the cwd worktree is detached — errors with the detached HEAD message above

Both are consistent with the worktree-by-branch model. If detached HEAD support for the cwd case is wanted, step_commit would need to use for_action_branchless() instead of for_action() — but that seems like a separate decision.

@max-sixty
Copy link
Copy Markdown
Owner

I think internally we should change to supply a worktree path, especially for commit, which doesn't work but worktree-less branches.

the external interface would remain the same

@worktrunk-bot
Copy link
Copy Markdown
Collaborator Author

Makes sense — CommandEnv would store and operate on worktree paths internally, with the --branch flag just being one way to resolve to a path (the other being cwd). That way commit could work in detached worktrees too, since the path is all git needs to stage and commit.

I'll rework for_branch() and for_action() to converge on a path-based internal model, keeping --branch as the external interface. The branch field becomes truly optional — populated when available but not required for the operation.

…commit

`for_action()` no longer requires a branch — it resolves the worktree path
from cwd and populates the branch only when available. Commands that need a
branch (merge, squash) already call `require_branch()` after construction.

This makes `wt step commit` work in detached HEAD worktrees, since git only
needs a worktree path to stage and commit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
src/cli/step.rs Outdated
Comment on lines +48 to +50
/// Branch to operate on
///
/// Defaults to current worktree.
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make these into a single line

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done — combined into /// Branch to operate on (defaults to current worktree).

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@max-sixty max-sixty merged commit 9160162 into main Mar 30, 2026
26 checks passed
@max-sixty max-sixty deleted the step-commit-branch-flag branch March 30, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants