Claude Code Git Workflow
Claude Code deeply integrates Git functionality, allowing you to perform almost all Git operations using natural language, including creating commits, managing branches, handling merge conflicts, and leveraging Git Worktree for parallel workflows. This chapter details how to efficiently use Git in Claude Code.\n\n* * *\n\n## Basic Git Operations\n\n### 1. Viewing Changes\n\nSimply ask using natural language to get the Git status:\n\nWhat files did I change? What are the contents of this change? What was changed in the last 10 commits? Check the current git status\nClaude will automatically execute the corresponding Git commands and display the results.\n\n### 2. Committing Code\n\nSimple commit:\n\nCommit this change Commit these modifications\nCommit with a description:\n\nCommit these modifications, with the commit message explaining the fix for the login validation bug\nClaude will automatically generate a standardized commit message based on the actual changes.\n\n### 3. Branch Management\n\nCreate a new feature/user-profile branch Switch to the develop branch Merge the latest changes from the main branch View all branches Delete merged branches\n\n* * *\n\n## Handling Merge Conflicts\n\n### 1. Automatically Resolving Simple Conflicts\n\nHelp me resolve the merge conflict This file has a conflict, help me check which version is correct\nClaude will analyze the conflict content and choose the appropriate version or propose a solution based on the project context.\n\n### 2. Manual Intervention for Complex Conflicts\n\nWhen the conflict is too complex, Claude will explain the pros and cons of each option to you:\n\nThis conflict involves architectural design, you decide which plan to keep: A. Keep our implementation... B. Keep the introduced optimization...\n> Claude Code will display the modifications and request your confirmation before executing any file changes. For high-risk operations like merge conflicts, it is even more important to carefully review every change.\n\n* * *\n\n## Git Worktree Parallel Workflow\n\nGit Worktree allows you to work on different branches simultaneously in different directories without switching branches or cloning the repository. This is one of the most powerful parallel working methods in Claude Code.\n\n### 1. Creating a Worktree Using Claude\n\nSpecify a worktree when starting Claude:\n\nclaude --worktree feature-auth\nThis will automatically:\n\n* Create a new directory in `.claude/worktrees/feature-auth/`\n* Create a new branch based on `origin/HEAD` (remote default branch)\n* Start an independent Claude session in the new directory\n\nOpen multiple worktrees simultaneously:\n\nclaude --worktree bugfix-123 claude --worktree\nWhen no name is specified, Claude will automatically generate a fun name (like `bright-running-fox`).\n\n### 2. Worktree Properties\n\n| Property | Value |\n| --- | --- |\n| Location | `/.claude/worktrees/` |\n| Branch naming | `worktree-` |\n| Base branch | The branch pointed to by remote `origin/HEAD` |\n\n### 3. Worktree Lifecycle\n\n| Scenario | Behavior |\n| --- | --- |\n| No modifications made | Worktree and branch are automatically deleted |\n| Changes or commits exist | Claude prompts you to choose to keep or delete |\n| Orphaned worktree due to Claude crash | Automatically deleted after exceeding the number of days set by `cleanupPeriodDays` |\n\n### 4. Copying .gitignore Files to Worktree\n\nCreate a `.worktreeinclude` file in the project root directory to specify the gitignore files that need to be copied to the worktree:\n\n.env .env.local config/secrets.json\nOnly files that match a pattern **and** are gitignored will be copied.\n\n### 5. Manually Managing Worktrees\n\nIf you need more control, you can manage them manually:\n\n# Create a worktree and specify a branch: git worktree add ../project-feature-a -b feature-a # Create a worktree from an existing branch git worktree add ../project-bugfix bugfix-123# in worktree Start Claude Code in ../project-feature-a && claude # Clean up after completion git worktree list git worktree remove ../project-feature-a\n> It is recommended to add `.claude/worktrees/` to `.gitignore` to prevent worktree contents from showing up as untracked files in the main repository.\n\n* * *\n\n## Sub-agent Worktree Isolation\n\nSub-agents can use the worktree isolation feature to achieve fully independent parallel work:\n\nMethod 1: Let Claude handle it automatically\n\nLet the sub-agent use a worktree to process these tasks in parallel\nMethod 2: Specify in the sub-agent configuration\n\n## Instance\n\n---\n\n name: experimental-refactor\n\n description: inTest refactoring approach in an isolated worktree\n\n isolation: worktree # inRun in a temporary worktree\n\n tools: Read, Write, Edit, Bash\n\n---\n\nYou can freely modify in the isolated environment without affecting the main branch.\n\n Summarize the changes and whether the plan was successful after completion.\n\n### Sub-agent Worktree Features\n\n* Each sub-agent automatically gets an independent worktree\n* Worktrees are automatically cleaned up after the task is completed\n* Modifications do not affect the main repository\n* Suitable for exploratory tasks and plan comparisons\n\n* * *\n\n## Pull Request Workflow\n\n### 1. Steps to Create a PR\n\n**Step 1: Ask Claude to summarize the changes**\n\nSummarize the changes I made to the authentication module\n**Step 2: Generate a PR**\n\nCreate a PR\n**Step 3: Refine the PR description**\n\nAdd more content about security improvements to the PR description\n### 2. PR Session Association\n\nWhen using `gh pr create` to create a PR, the Claude session is automatically associated with that PR:\n\n* Once the session is linked to the PR, you can resume the conversation from the PR\n* Use `claude --from-pr ` to resume the associated session\n\n### 3. Using GitHub CLI\n\nClaude knows how to use the `gh` CLI tool. If `gh` is not installed, Claude can read and write to the GitHub API, but with limited functionality.\n\n# Claude Available command: gh pr create --title "Fix login bug" gh pr view --comments gh pr diff gh issue create --title "Bug report"\n\n* * *\n\n## Session Management and Git Integration\n\n### 1. Resuming Previous Sessions\n\n| Command | Function |\n| --- | --- |\n| `claude --continue` | Continue the most recent conversation in the current directory |\n| `claude --resume` | Open the session selector or resume by name |\n| `claude --from-pr 123` | Resume the session associated with a specific PR |\n\n### 2. Session Selector Git Features\n\n| Shortcut | Function |\n| --- | --- |\n| `B` | Filter sessions for the current git branch |\n\n### 3. Session Metadata Display\n\nThe session selector displays the following information:\n\n* Session name or initial prompt\n* Time since last activity\n* Number of messages\n* Git branch (if any)\n\n### 4. Branch Sessions\n\nSessions created using `/branch`, `/rewind`, or `--fork-session` are grouped under the root session for easy management.\n\n* * *\n\n## Plan Mode and Security Analysis\n\nPlan Mode uses read-only operations to safely analyze the codebase without executing any write operations.\n\n### Starting Plan Mode\n\nclaude --permission-mode plan\nOr run a one-time query within a session:\n\nclaude --permission-mode plan -p "Analyze the authentication system and propose improvements"\n### Configuring Default Mode in settings.json\n\n## Instance\n\n{\n\n"permissions":{\n\n"defaultMode":"plan"\n\n}\n\n}\n\n* * *\n\n## Typical Workflow Examples\n\n### Workflow 1: Multi-task Parallel Development\n\n# Scenario: Develop three features concurrently without switching branches> Launch three worktrees to handle login refactoring, payment integration, and performance optimization separately> inWork independently in each worktree, then merge into the main branch upon completion\n### Workflow 2: Bug Fix Process\n\n# Step 1: Describe the problem> User feedback: Page still shows user as logged in after logout and refresh# Step 2: Create a fix branch> Create a bugfix/session-cookie branch to resolve this issue# Step 3: Analysis and Fix> First analyze potential causes, then fix in the bugfix branch# Step 4: Commit and create a PR> Commit the fix and create a PR\n### Workflow 3: Code Review\n\n# Review specific files> Help me review src/payment/processor.tsοΌFocus on error handling# Review git changes> Review all recent changes to identify any obvious issues# inReview in a separate branch> Create a worktree to review this refactoring plan\n### Workflow 4: Feature Development and Validation\n\n# Main session: Implement new features> Implement user profile editing functionality# Sub-agent: Run tests in parallel> inRun all tests in the sub-agent, returning only failed tests and root causes# Sub-agent: Check code standards> Use a sub-agent to verify code compliance with project standards# Upon completion: Create a PR> Create a PR with a detailed description\n\n* * *\n\n## Non-Git Version Control Systems\n\nFor SVN, Perforce, or Mercurial users, Claude Code supports extension through hooks:\n\n### Configuring Custom Hooks\n\nConfigure in `.claude/settings.json`:\n\n## Instance\n\n{\n\n"hooks":{\n\n"WorktreeCreate":"./scripts/create-worktree.sh",\n\n"WorktreeRemove":"./scripts/remove-worktree.sh"\n\n}\n\n}\n\nThese hooks replace the default git behavior. When using hook scripts, copy local configuration files inside the script instead of using `.worktreeinclude`.\n\n* * *\n\n## Anthropic Internal Team Best Practices\n\n* **Open multiple worktrees**: Run 3-5 git worktrees simultaneously, each with an independent session. This is a universally acknowledged best practice for efficiency within the team\n* **Plan before complex tasks**: Use `plan` mode to have one Claude write the plan, and another act as an advisor to review it\n*
YouTip