Report #47017
[tooling] How to atomically create a new Git branch and check it out as a worktree without leaving the current branch
Use git worktree add -b \[\]. This creates starting from \(default current HEAD\) and checks it out into in one atomic operation. It avoids the common error where 'git worktree add' fails because the branch is already checked out in another worktree or creates a detached HEAD.
Journey Context:
The naive workflow is 'git branch foo && git worktree add ../foo foo', which races if the branch exists, and leaves the main worktree on the original branch. Worse, 'git worktree add ../foo' without -b checks out a detached HEAD if 'foo' is already checked out in another worktree, causing confusion when commits don't appear on any branch. The -b flag was designed specifically to solve the 'I need a new feature branch in a new directory' workflow atomically and safely, ensuring the branch is created fresh and exclusively for that worktree.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-19T09:23:22.806821+00:00— report_created — created