# Claude Code git workflow: branches, commits and PRs

> Canonical: https://skillalmanac.com/learn/claude-code-git-workflow | Updated: 2026-11-27 | Index status: index-eligible

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.

## Facts

- 2026-11-26: `claude --worktree <name>` by default creates a worktree under `.claude/worktrees/<name>/` on a new branch named `worktree-<name>`, branched from the repository's default branch (https://code.claude.com/docs/en/worktrees)
- 2026-11-26: A `.worktreeinclude` file uses .gitignore syntax and copies only gitignored files that match a pattern into new worktrees (https://code.claude.com/docs/en/worktrees)
- 2026-11-26: On exit, an unnamed session with a clean worktree removes its worktree and branch automatically; a named session prompts first, and a worktree with work in it prompts keep or remove (https://code.claude.com/docs/en/worktrees)
- 2026-11-26: A kept worktree is resumed with `claude --worktree <name> --resume <session-id>` from the main checkout, using the session ID printed on exit (https://code.claude.com/docs/en/worktrees)
- 2026-11-26: The repository needs at least one commit before a worktree can be created, and `claude --permission-mode plan` makes no edits until the plan is approved (https://code.claude.com/docs/en/common-workflows)
- 2026-11-26: `git worktree add -b <new-branch> <path>` creates a new branch and refuses if the branch already exists (https://git-scm.com/docs/git-worktree)
- 2026-11-26: `git worktree remove` removes only a clean worktree; `--force` removes an unclean one, and a locked worktree needs `git worktree unlock` first or `--force` twice (https://git-scm.com/docs/git-worktree)
- 2026-11-27: `git branch -d` deletes a branch only when it is fully merged in its upstream branch, or in HEAD if no upstream is set, and `git branch --merged <commit>` lists only branches whose tips are reachable from that commit (https://git-scm.com/docs/git-branch)

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](/skills/skill-almanac/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](/skills/skill-almanac/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.

## Related skills

- [git-worktree-and-pr-flow](https://skillalmanac.com/skills/skill-almanac/git-worktree-and-pr-flow.md): Platform Score 56/100 (provisional)
- [commit-message-linter](https://skillalmanac.com/skills/skill-almanac/commit-message-linter.md): Platform Score 46/100 (provisional)

## Related guides

- [Claude Code Desktop App: What the Code Tab Adds](https://skillalmanac.com/learn/claude-code-desktop-app-guide.md)
- [Cursor vs Claude Code: Editor or Terminal Workflow](https://skillalmanac.com/learn/cursor-vs-claude-code.md)
- [Claude Code first session: a plan, edit, review loop](https://skillalmanac.com/learn/claude-code-first-session-workflow.md)
- [What Is Claude Code and What It Can Touch on Your Machine](https://skillalmanac.com/learn/what-is-claude-code.md)

## Sources

- https://code.claude.com/docs/en/common-workflows
- https://code.claude.com/docs/en/worktrees
- https://git-scm.com/docs/git-worktree
- https://git-scm.com/docs/git-branch

Platform Scores report our assessment under a published method. They are not a safety guarantee.
