Feature request: Create or switch to branch
I'd like a version of wt switch -c branch_name that does not abort if the branch already exists.
I can build this if it is accepted.
Thanks for the suggestion! I searched the codebase and confirmed there's no existing flag for this behavior.
Currently wt switch -c branch fails with "Branch already exists" if the branch is already present, and hints to re-run without --create. The validation lives in resolve_switch_target() in src/commands/worktree/switch.rs.
For comparison, git itself has git switch -C (uppercase) which is "create or force-reset." An idempotent create-or-switch variant (without the force-reset semantics) would be a distinct behavior — essentially "create if missing, switch if exists."
I'll leave it for a maintainer to evaluate and prioritize.
@worktrunk-bot please find the existing issue for this
I searched through all open and closed issues (by title and keyword: "create or switch", "idempotent", "switch -C", "branch already exists", "force create") and couldn't find a prior issue for this specific feature. The closest related discussion is #518 ("should we require --create?"), which debated whether --create should be required at all — but that's about the create/switch ambiguity, not about an idempotent create-or-switch flag.
This appears to be the first issue requesting this behavior.
Is your suggestion for now to use a shell alias/wrapper? I can live with that.
I didn't mean to suggest a specific workaround — I was just analyzing the codebase to help the maintainer evaluate the request. A shell wrapper like wt switch -c "$branch" 2>/dev/null || wt switch "$branch" would work as a stopgap, but the decision on whether to add a native flag (e.g., -C for idempotent create-or-switch) is up to @max-sixty.
yes #518 is it!
yes, check out https://worktrunk.dev/step/#aliases (also shell alias is OK, up to you obv)
for context @jonashaag, I'm v keen to prevent config explosion, where wt becomes a library of possible options, and also to avoid removing options. So the aliases should let folks test out workflows, and then if there is something that someone has found helpful for a month, and others find it helpful, then we can add it on. hope that's reasonable
@worktrunk-bot please could you:
- draft and test an alias for @jonashaag that does what he's like
- open a PR adding to your
/tendguidance to suggest aliases iff they could be helpful to folks (and to invest the time to compose and test them)
@jonashaag here's a tested create-or-switch alias. Two options depending on your preference:
Option 1: wt step alias — add to ~/.config/worktrunk/config.toml:
[aliases]
sw = "wt switch -c {{ name }} 2>/dev/null || wt switch {{ name }}"
Usage: wt step sw --var name=my-branch
Option 2: Shell function (more ergonomic for positional args):
# Add to your .bashrc / .zshrc
wts() { wt switch -c "$1" 2>/dev/null || wt switch "$1"; }
Usage: wts my-branch
Both create the branch and worktree if it doesn't exist, or switch to it if it does. I tested both paths (new branch + existing branch) and they work correctly.
See Aliases for more on the alias system.
I also opened #1728 to add guidance so future triage suggests aliases when they can address a feature request.
@jonashaag Here's a shell function that does idempotent create-or-switch:
wts() { wt switch -c "$@" 2>/dev/null || wt switch "$@"; }
This tries --create first; if the branch already exists it falls back to a regular switch. Works for both new and existing branches:
$ wts feature-x # creates branch + worktree (new)
$ wts feature-x # switches to existing worktree (idempotent)
Add it to your .bashrc/.zshrc, or see the aliases docs for the wt step alias system (which uses template variables from the current worktree rather than positional args, so a shell function is the better fit here).
I've tested both directions — this handles all the edge cases correctly.
Sounds good.
Great idea with the bot btw. Will steal from you for our internal repos.
Great idea with the bot btw. Will steal from you for our internal repos.
thanks! extracted it yesterday to https://github.com/max-sixty/tend, but need a couple of days before it's worth adopting (or ofc feel free to make your own, but was actually a surprising amount of work to get it decent...)
@worktrunk-bot debug the double-post from above, put an issue into the tend repo.
I think probably:
- you got two events
- both saw both posts, started composing
- the duplication check happened, but both were posted at exactly the same time (at least the timestamps are identical, albeit to 1 min)
post to tend ask it to consider options for an alternative design; should be simple even if a bit inefficient
Debugged the double-post. Filed https://github.com/max-sixty/tend/issues/61 with analysis and fix options.
Root cause: The concurrency / cancel-in-progress is on the verify job (which completes in ~2s), not on handle (which takes minutes). Your two comments were 70s apart — both verify jobs finished before the other arrived, so both handle jobs ran concurrently and posted independently.
Fix: Move the concurrency group to the handle job. The later event cancels the earlier one, which is the right behavior since it has strictly more context.
Timeline
| Time | Event |
|---|---|
| 19:52:36Z | Comment 1 posted → triggers run A |
| 19:52:44Z | Run A verify completes (2s) |
| 19:53:46Z | Comment 2 posted → triggers run B |
| 19:53:55Z | Run B verify completes (nothing to cancel) |
| 19:52:48Z | Run A handle starts |
| 19:53:58Z | Run B handle starts (concurrent) |
| 20:00:11Z | Run A posts comment |
| 20:00:35Z | Run B posts comment (duplicate) |