[Feature Request] Support selecting base branch when creating git worktree

Open 💬 22 comments Opened Feb 6, 2026 by denysdanyliukboosters

Bug Description
I have a feature request for claude app -> claude code section.
When working with worktree i want to be able to select base branch for this worktree like it was done in codex app. This will be very helpfull.

Environment Info

  • Platform: darwin
  • Terminal: Apple_Terminal
  • Version: 2.1.34
  • Feedback ID: 40c08d3e-f600-4585-b62c-5634e7ddaa9f

Errors

[]

View original on GitHub ↗

22 Comments

Orinks · 4 months ago

Seconding this — particularly important for automated/headless workflows where Claude Code is invoked via --worktree to implement GitHub issues.

Many repos use a dev (or develop) branch as the integration target rather than main. Without a --base-branch option, worktrees always branch from main, which means PRs either target the wrong branch or require manual rebasing after the fact. The workaround of ensuring the main worktree has dev checked out before invoking Claude is fragile in parallel/concurrent setups.

A --base-branch <branch> flag (or equivalent config in agent definitions via isolation: worktree) would make this work cleanly without workarounds. The WorktreeCreate hook can paper over it for now, but a first-class option would be much better.

dariusrosendahl · 4 months ago

This would be greatly appreciated. I actually wrote a proposal but it was a duplicate of this issue:

Feature Request

When using claude --worktree (or claude -w), the new worktree always branches from the default remote branch (e.g., main). There's no way to create a worktree based on the current branch/HEAD.

Use Case

When working on a feature branch and wanting to spin up an isolated Claude session to work on a subtask or related change, it would be useful to branch from the current branch rather than main. Currently, the workaround is to manually create a worktree with git and then start Claude in it:

git worktree add .claude/worktrees/my-feature -b worktree-my-feature HEAD
cd .claude/worktrees/my-feature && claude

This loses the automatic cleanup prompt and other niceties of --worktree.

Proposal

Add a --worktree-base flag (or similar) to specify the base branch/ref:

# Branch from current HEAD
claude -w my-feature --worktree-base HEAD

# Branch from a specific branch
claude -w my-feature --worktree-base feature/auth

Alternatively, a simpler approach: make --worktree branch from HEAD by default (matching git worktree add behavior) instead of always using the default remote branch. This would be the least surprising behavior for users already on a feature branch.

A middle ground could be a setting in ~/.claude/settings.json:

{
  "worktree": {
    "baseBranch": "HEAD"
  }
}

Current Behavior

claude --worktree always branches from the default remote branch (e.g., origin/main).

Expected Behavior

Users should be able to control which branch/ref the worktree is based on, either via a CLI flag or a setting.

chrisjenx · 4 months ago

Is this the same issue/related, to where when creating a new work tree (in CC) it will create it from head instead of the base branch. I.e. creating a new work tree from master/main should take from that branch, but it seems to take the commits from head instead?

Orinks · 4 months ago

Slightly related. I should be able to choose the base branch that worktrees inherit from. Right now it only inherits from main/master/default branch.

________________________________
From: Christopher Jenkins @.*>
Sent: Wednesday, February 25, 2026 12:51 PM
To: anthropics/claude-code
@.*>
Cc: Orinks @.>; Comment @.>
Subject: Re: [anthropics/claude-code] [Feature Request] Support selecting base branch when creating git worktree (Issue #23622)

[https://avatars.githubusercontent.com/u/1167793?s=20&v=4]chrisjenx left a comment (anthropics/claude-code#23622)<https://github.com/anthropics/claude-code/issues/23622#issuecomment-3960967415>

Is this the same issue/related, to where when creating a new work tree (in CC) it will create it from head instead of the base branch. I.e. creating a new work tree from master/main should take from that branch, but it seems to take the commits from head instead?


Reply to this email directly, view it on GitHub<https://github.com/anthropics/claude-code/issues/23622#issuecomment-3960967415>, or unsubscribe<https://github.com/notifications/unsubscribe-auth/AJFLE3EPP5HNKZTYP63EYDT4NXOJ3AVCNFSM6AAAAACUGNSW3OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZTSNRQHE3DONBRGU>.
You are receiving this because you commented.Message ID: @.***>

Tydog99 · 4 months ago

+1 to this feature request. This bit me a few times today when I was expecting claude --worktree to add worktrees from the feature branch it was invoked from.

jspicher · 4 months ago

+1 👍

ieggel · 4 months ago

exactly what i wanted to ask for.

eggthem17 · 4 months ago

I need this too

servatti · 4 months ago

+1. The desktop app has a similar issue when selecting the base branch https://github.com/anthropics/claude-code/issues/23626.

spencerby28 · 3 months ago

Built a skill for now. Claude's smart enough to just use git fundamentals.
https://skills.sb28.ai/cc-worktree-workaround

Horacehxw · 3 months ago

Additional use case: non-origin remote feature branches

I frequently work on long-lived feature branches tracked on non-origin remotes (e.g. upstream/feat/perf-database). EnterWorktree always branches from HEAD, which points to main — not the feature branch I need.

Current workaround

git worktree add .claude/worktrees/<name> -b worktree-<name> upstream/feat/perf-database
cd .claude/worktrees/<name>

This loses session integration (automatic CWD switch, cleanup prompt, ExitWorktree support).

Proposed base parameter

For the EnterWorktree tool:

{
  "name": "base",
  "description": "Git ref (branch, tag, or commit) to base the new worktree on. Defaults to HEAD if omitted.",
  "type": "string"
}

Implementation notes:

  • git worktree add already accepts <commit-ish> as the last arg — this is a minimal change
  • Validate with git rev-parse --verify <base> before creation
  • If validation fails, suggest git fetch <remote> in the error message
  • ExitWorktree needs no changes (operates on path, not base)
  • Full backward compatibility: omitting base preserves current HEAD behavior

For the CLI --worktree flag (related to #27876):

claude -w my-feature --worktree-base upstream/feat/perf-database

This would cover both the tool-level and CLI-level entry points.

underspirit · 3 months ago

I desperately need this feature.

HelloThisIsFlo · 3 months ago

As a workaround, I’m using this hook

#!/bin/bash
# WorktreeCreate hook: branch worktrees from local HEAD instead of origin/HEAD.
#
# Without this hook, Claude Code bases worktrees on the remote tracking branch,
# so unpushed local commits aren't available to agents.
#
# Workaround for: https://github.com/anthropics/claude-code/issues/23622
#
# Input: JSON on stdin with session_id, cwd, hook_event_name
# Output: worktree path on stdout

set -euo pipefail

# Parse input
INPUT=$(cat)
CWD=$(echo "$INPUT" | jq -r '.cwd // empty')

if [ -z "$CWD" ]; then
  echo "ERROR: No cwd in hook input" >&2
  exit 2
fi

cd "$CWD"

# Generate unique worktree name
AGENT_ID="agent-$(openssl rand -hex 4)"
WORKTREE_PATH="$CWD/.claude/worktrees/$AGENT_ID"
BRANCH_NAME="worktree-$AGENT_ID"

# Create worktree from local HEAD
mkdir -p "$(dirname "$WORKTREE_PATH")"
git worktree add "$WORKTREE_PATH" -b "$BRANCH_NAME" HEAD >/dev/null 2>&1

# Return the path to Claude Code
echo "$WORKTREE_PATH"

And then in the config

{
  "hooks": {
    "WorktreeCreate": [
      {
        "hooks": [
          {
            "type": "command",
            "command": "$HOME/.claude/hooks/worktree-from-head.sh",
            "timeout": 30
          }
        ]
      }
    ]
  }
}

Note: On my machine the $HOME path is actually hardcoded, so if the hook doesn’t work try hardcoding the full path. _(I’m not home now, so I can’t test)_

Blackclaws · 3 months ago

I honestly thought that was just the default behaviour.... Cost me two expensive rebases.

notetiene · 2 months ago

Seriously it should be changed to use current branch at least.

Even when making a script, we're pretty limited without relying on hacky tricks.

softmarshmallow · 2 months ago

Need UI in Claude Code Desktop for this.
very anoying.

chrisjenx · 2 months ago

It seems like this UI is now there, just super flipping buggy, for some reson it misses origin branches sometimes.

danlzh · 2 months ago

+1 for this feature

danlzh · 2 months ago

Thanks @HelloThisIsFlo for sharing the script, seems to work fine.

Added a small change to the script use the worktree name passed in by user

#!/bin/bash
# WorktreeCreate hook: branch worktrees from local HEAD instead of origin/HEAD.
#
# Without this hook, Claude Code bases worktrees on the remote tracking branch,
# so unpushed local commits aren't available to agents.
#
# Workaround for: https://github.com/anthropics/claude-code/issues/23622
#
# Input: 
# JSON on stdin with session_id, cwd, hook_event_name, name
#
# Example Input: 
# {"session_id":"0c9bda6e-...","transcript_path":"/path/to/transcript/0c9bda6e-....jsonl","cwd":"/path/to/cwd","hook_event_name":"WorktreeCreate","name":"test"}
# 
# Output: 
# worktree path on stdout

set -euo pipefail

# Parse input
INPUT=$(cat)
CWD=$(echo "$INPUT" | jq -r '.cwd // empty')

if [ -z "$CWD" ]; then
  echo "ERROR: No cwd in hook input" >&2
  exit 2
fi

# Parse worktree name
WORKTREE_NAME=$(echo "$INPUT" | jq -r '.name // empty')
if [ -z "$WORKTREE_NAME" ]; then
  echo "ERROR: No worktree name in hook input" >&2
  exit 2
fi

# Move to project directory
cd "$CWD"

# Generate unique worktree name
AGENT_ID="agent-$(openssl rand -hex 4)"
WORTREE_NAME_WITH_AGENT_ID="$WORKTREE_NAME-$AGENT_ID"
WORKTREE_PATH="$CWD/.claude/worktrees/$WORTREE_NAME_WITH_AGENT_ID"
BRANCH_NAME="worktree/$WORTREE_NAME_WITH_AGENT_ID"

# Create worktree from local HEAD
mkdir -p "$(dirname "$WORKTREE_PATH")"
git worktree add "$WORKTREE_PATH" -b "$BRANCH_NAME" HEAD >/dev/null 2>&1

# Return the path to Claude Code
echo "$WORKTREE_PATH"
danlzh · 2 months ago

Claude code now natively support setting worktree baseref to local HEAD since 2.1.133

See CHANGELOG https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md:

2.1.133
- Added worktree.baseRef setting (fresh | head) to choose whether --worktree, EnterWorktree, and agent-isolation worktrees branch from origin/<default> or local HEAD. Note: the default fresh changes EnterWorktree's base back to origin/<default> (it has been local HEAD since 2.1.128) — set worktree.baseRef: "head" to keep unpushed commits in new worktrees

You can just add this to ~/.calude/settings.json now:

"worktree": {
    "baseRef": "head"
}
agroene · 1 month ago

+1 specifically for the Agent tool's isolation: "worktree" mode — same bug as the CLI --worktree flag, same fix needed. The thread is mostly framed around CLI invocation; documenting the programmatic-Agent angle here so it isn't lost.

Concrete impact from a real session today

Workflow: solo developer, intentional batched-push pattern (epic branches commit locally, single push when the whole epic ships). Local master was 23 commits ahead of origin/master, including 4 shipped feature implementations and a ready-for-dev tech-spec the Agent was supposed to implement.

Spawned an Agent with isolation: "worktree". The worktree was created from origin/master, not local HEAD. Reflog evidence (three worktrees from the session):

worktree-agent-a3fb914519cde564d  1c198e4: branch: Created from origin/master  (2026-05-18)
worktree-agent-a9d853104e1b9b811  1c198e4: branch: Created from origin/master  (2026-05-19)
worktree-agent-ad1dd233fc3fbda93  32fe5b8: branch: Created from origin/master  (2026-05-13)

origin/master was at a 6-day-old commit. The Agent's worktree was missing:

  • the 4 feature implementations its spec depends on
  • the spec itself
  • test fixtures the spec references
  • a PULL_REQUEST_TEMPLATE.md extension the spec required

Net result: 22 tool calls wasted, no usable output. Even if the Agent had completed within its usage limit, the work would have been unmergeable because it was rooted on a baseline 23 commits behind reality.

Proposed fix (endorsing @dariusrosendahl's earlier shape)

For the Agent tool specifically, an object form on isolation would be ergonomic:

Agent({ isolation: { type: "worktree", base: "HEAD" } })

…and the same worktree.baseBranch setting would cover both the CLI flag and the Agent tool.

Strong preference for HEAD as the default rather than origin/<default-branch>:

  • matches upstream git worktree add <path> behaviour
  • least surprising for users on a feature branch
  • doesn't silently penalise local-first workflows (the exact pattern that bit us here)
  • the user can always override to origin/main if they explicitly want the remote baseline

The current default of origin/<default> is the least-safe option: it silently produces output rooted in stale state without ever surfacing the divergence to the user. At minimum, the Agent tool could refuse to create a worktree if local <branch> is ahead of origin/<branch> and warn the user — but the cleaner fix is just to base on HEAD.

Kagami · 8 days ago
Claude code now natively support setting worktree baseref to local HEAD since 2.1.133 See CHANGELOG https://github.com/anthropics/claude-code/blob/main/CHANGELOG.md: `` 2.1.133 - Added worktree.baseRef setting (fresh | head) to choose whether --worktree, EnterWorktree, and agent-isolation worktrees branch from origin/<default> or local HEAD. Note: the default fresh changes EnterWorktree's base back to origin/<default> (it has been local HEAD since 2.1.128) — set worktree.baseRef: "head" to keep unpushed commits in new worktrees ` You can just add this to ~/.calude/settings.json now: ` "worktree": { "baseRef": "head" } ``

I think the CLI flag still would be very useful.

Sometimes I want to do sub-feature on top of the current HEAD, sometimes I want to start from scratch (origin/HEAD). Editing settings every time is not convenient, CLI flag would work the best here.