Compound cd && git commands in worktrees should not require approval
Summary
When working in git worktrees, Claude Code flags cd <worktree-path> && git <command> as requiring user approval with the message:
Compound commands with cd and git require approval to prevent bare repository attacks
This is overly broad when the target directory is a known worktree of the current repository (e.g., under .claude/worktrees/).
The inconsistency
git -C <path> <command> does exactly the same thing as cd <path> && git <command> — both run git commands in a different directory. However, only the cd && form triggers the security approval. This means:
git -C /some/untrusted/path status— no approval requiredcd /some/untrusted/path && git status— approval required
Both can target unexpected repositories equally, so the security check is inconsistent.
The worktree problem
When Claude Code spawns agents in worktrees (via isolation: "worktree" or EnterWorktree), the agent needs to run git commands in the worktree directory. Since the worktree is a subdirectory of the original repo (.claude/worktrees/<name>), it's not an "unexpected repository" — it's the same repo, just a different working copy.
Currently, every git command in a worktree triggers an approval prompt, which defeats the purpose of autonomous worktree-based agents.
Suggested fix
The security check should recognize worktree paths as safe. Specifically:
- If the target of
cdis a git worktree of the current repository (verifiable viagit worktree list), it should be treated the same as running git commands in the main working directory. - Alternatively, if
git -Cis not going to be checked,cd && gitshouldn't be checked either — the current inconsistency provides a false sense of security.
Reproduction
- Create a worktree:
git worktree add .claude/worktrees/test-branch - Have Claude Code run:
cd .claude/worktrees/test-branch && git status - Observe the approval prompt about bare repository attacks
- Compare with:
git -C .claude/worktrees/test-branch status(no prompt)
Environment
- Claude Code CLI
- Any git repository using worktrees
8 Comments
Clarification: The
cd && gitpattern should really only be needed once when an agent first enters a worktree. After that initialcd, the working directory persists across Bash tool calls, so subsequent git commands don't need thecdprefix at all.So the scope of this issue is narrower than originally described — it's specifically about that single initial directory change when entering a worktree. But the inconsistency between
git -Candcd && gitstill stands.Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
Because of this issue, Claude Code requires confirmation for every git operation. Needless to say that's rather annoying.
Slows down the development process on Windows extremely as Claude asks for permission on almost every operation while researching, planning and reviewing
Even without explicit extra worktrees, this is a significant problem when working in windows at a folder lower than the repo root.
If you work claude out of
c:\repos\repo-root\productWhen it wants to look at git history, it tries to operate at the pwd, and fails.
Then every command is
cd c:\repos\repo-root && git ...This requires permission prompting and makes researching and planning impossible to leave unattended.
Can't agree more. This is a real frustration.
To get around it, I've built a hook that detects compound commands and redirects Claude to first
cdinto the directory, and then run the relevant commands.It works surprisingly well, but frustrating that I had to do it at all.
The hook I've been using
I'd be willing to bet someone has a better solution for this 😄
This is especially painful with
isolation: "worktree"on subagents. The Agent tool creates a worktree at/tmp/wt-*specifically so the agent can work autonomously, but then every single operation in that worktree — reads, writes,cd && git status— requires manual approval because the path is outside the project directory.This completely defeats the purpose of worktree isolation. The user ends up babysitting a stream of approval prompts for an agent that's supposed to be autonomous.
Worktree paths created by Claude Code itself should automatically inherit the parent project's permissions.
Root cause diagnosis: Claude Code's permission matcher evaluates each rule against the entire command string.
Bash(git *)matchesgit statusbut notcd /path && git statusbecause the string doesn't start withgit. This is a literal string match limitation, not a security decision —git -C /path statusbypasses it entirely, as OP noted.Hook workaround (PermissionRequest):
A
PermissionRequesthook can parse compound commands, verify each part is safe, and auto-approve:The hook splits on
&&,;,||, checks each component against a safe list (cd,git status/log/diff/add/commit/...), and only approves if all parts are recognized:Key design choice: This is approve-based, not deny-based. If any component is unrecognized, the hook stays silent and falls through to the normal prompt — so it can't approve something dangerous by mistake.
For worktree users specifically: Since the initial
cd <worktree-path>is always followed by git commands, this hook eliminates the approval prompt for the exact pattern described in this issue.Full tested version with 13 tests:
auto-approve-compound-git.sh