Claude Code git workflow: branches, commits and PRs

Keep Claude Code edits reviewable: one worktree and one branch per task, a review step before commit, and a clean exit that leaves no stray branches.

Updated · Markdown version

Keep each Claude Code task on its own branch in its own worktree, so the agent's edits stay out of your main checkout until you decide what to commit. The steps use the Claude Code flags and git commands documented as of 2026-11-26. The git commands were also run that day in a scratch repository with git 2.50.1.

Set up one worktree per task

  1. Confirm the repository has at least one commit. The common workflows page notes that a worktree is created from an existing commit.
  2. Add .claude/worktrees/ to .gitignore. Otherwise the worktree contents appear as untracked files in the main checkout.
  3. Add a .worktreeinclude file at the project root, listing files such as .env and config/secrets.json. Entries use .gitignore syntax, and only gitignored files that match are copied. Tracked files are never duplicated. Do this before you create the worktree: the file applies to new worktrees. In a scratch git repository on 2026-11-27, an ignored .env did not appear in a worktree that already existed when .worktreeinclude was added.
  4. Start the session with claude --worktree feature-auth. By default, Claude creates .claude/worktrees/feature-auth/ at the repository root on a new branch named worktree-feature-auth. Name the worktree after the task so the branch name says what it holds.
  5. Run your install step inside the new worktree. A worktree is a fresh checkout, so dependencies are not there yet.
  6. For a task where you want to approve the plan first, add the plan flag to the same launch in step 4: claude --worktree feature-auth --permission-mode plan. The two flags are documented separately, and this line combines them. The agent reads and proposes, and makes no edits until you approve.

Create a branch by hand when you need a specific base

A worktree made with --worktree branches from the repository's default branch unless the worktree.baseRef setting is "head", and that setting does not accept a branch name. To start from a specific branch or to place the worktree outside the repository, use git directly:

git worktree add -b feature-x ../feature-x main
git worktree list

The first command creates feature-x from main and checks it out in ../feature-x. The second lists the main worktree first, then each linked worktree, with its path, the commit checked out and the branch.

Review, commit and open the PR from the worktree

Review the full diff before you commit, and commit only the files that belong to the task. To open the pull request, ask Claude to create one ("create a pr for my changes") or run gh pr create yourself. The common workflows page says Claude Code links the session to the PR when Claude creates it with gh pr create. Read the generated description before you submit it.

Exit and clean up

When you exit an interactive worktree session, Claude checks for work that removal would delete, including changed or untracked files and new commits. An unnamed session with a clean worktree removes the worktree and its branch on its own. A named session with a clean worktree prompts first. A worktree with work in it prompts you to keep or remove it, and removing deletes the directory, its branch and the work in them. If you keep it, return later with claude --worktree <name> --resume <session-id> from the main checkout, using the session ID printed on exit.

To remove a worktree you created by hand, run git worktree remove ../feature-x. This works only on a clean worktree, one with no untracked files and no changes to tracked files. Add --force (or -f) to remove an unclean one, which deletes that work. If git reports the worktree as locked, run git worktree unlock ../feature-x and retry, or pass --force twice.

git worktree remove takes away the directory, not the branch. In the scratch run, git branch still listed feature-x after the worktree was removed. Delete a merged branch with:

git branch -d feature-x

-d refuses a branch that is not fully merged into its upstream branch, or into HEAD when no upstream is set. That check is not proof that the work reached main: a branch that was pushed and matches its own upstream passes it without being merged anywhere else. Before you delete, confirm the target you care about. git branch --merged main lists the branches whose tips are reachable from main, and the task branch should be in that list.

Check that the setup worked: git worktree list should show the task worktree on its own branch, and git status in the main checkout should show none of the agent's files. After cleanup, git worktree list and git branch should no longer show the task.

Pitfalls

  1. Edits land in the main checkout. A session is isolated when it starts with --worktree (without --tmux), or when Claude moves it into a worktree with its EnterWorktree tool. You can tell it happened when git status in the main checkout lists files you did not touch. Move or discard those changes, then restart the session with --worktree.
  2. The app fails to start in the worktree. Check for a missing .env first: untracked files such as .env are not present in a new worktree. Add the file to .worktreeinclude and create a new worktree, or copy it in by hand. A .worktreeinclude entry added later does not fill a worktree that already exists.
  3. `git worktree add` refuses the branch. By default git does not check out a branch that is already checked out in another worktree. Create a new branch with -b instead. -b in turn refuses a branch name that already exists, so pick a new name.
  4. Work disappears on exit, or a session never prompts. An unnamed session removes a clean worktree without asking. If you plan to return to a task, name the session, so Claude prompts before it removes the worktree.
  5. Removal refuses a worktree. A locked worktree needs git worktree unlock first, and an unclean one needs --force. Commit the work or unlock the worktree before you retry.

When to use a skill instead

One small change needs no more structure than a direct prompt and a diff review. Packaging the routine may be worth trying when the same branch and PR steps repeat across many tasks.

git-worktree-and-pr-flow sets one rule (one task, one worktree, one branch, one PR), supplies the commands for it, and checks pull request size against numeric limits. Its page lists efficacy as LIFT from a single run on 3 tasks (2026-11-21) and marks that result as provisional, so read it as an early signal, not a full measurement.

commit-message-linter is a related skill that checks commit messages saved in a text file against Conventional Commits rules and reports each finding. It does not run git, so you export the messages first. No comparison with and without this skill has been run, so its effect is not measured.

FAQ

Does Claude Code open pull requests without being asked?

The documented flow starts with a request from you. The common workflows page describes creating pull requests by asking Claude directly, and it recommends reviewing the generated PR before you submit it.

Can two Claude Code sessions edit one repository at the same time?

Yes, if each runs in its own worktree. Start the second one in another terminal with a different name, such as claude --worktree bugfix-login. Without worktrees, both sessions edit the same checkout and their changes can collide.

How do I return to a worktree session after I exit?

If you kept the worktree on exit, run claude --worktree <name> --resume <session-id> from the main checkout, with the session ID printed on exit. A session that ended inside its worktree without exiting it goes back to that worktree when you resume it with claude --resume from the main checkout.

Platform Score is our assessment under a published method. It is not a safety guarantee.

Next steps

  1. Claude Code Desktop App: What the Code Tab Adds Read next.
  2. Cursor vs Claude Code: Editor or Terminal Workflow Read next.
  3. Claude Code compatibility Install evidence per tool.
  4. All guides by topic Browse everything.

Sources