Git Workflow Automation in Claude Code

May 12, 2025 · View on GitHub

Overview

Git workflow automation in Claude Code provides structured, systematic approaches for handling version control tasks. Rather than executing simple git commands, Claude follows comprehensive workflows for commits, pull requests, and other git operations. These workflows ensure consistency, thoroughness, and adherence to best practices when manipulating code repositories.

Commit Workflow

Structure and Process

The git commit workflow consists of four main phases:

  1. Information Gathering: Collecting repository state
  2. Analysis: Reviewing changes and drafting a commit message
  3. Execution: Performing the commit operation
  4. Verification: Ensuring the commit succeeded

Phase 1: Information Gathering

Claude first runs these commands in parallel via Batch:

{
  "description": "Gather git information",
  "invocations": [
    {
      "tool_name": "Bash",
      "input": { "command": "git status" }
    },
    {
      "tool_name": "Bash",
      "input": { "command": "git diff" }
    },
    {
      "tool_name": "Bash",
      "input": { "command": "git log -n 5" }
    }
  ]
}

This provides Claude with:

  • Untracked and modified files
  • Exact nature of the changes
  • Recent commit history to match style

Phase 2: Analysis and Message Creation

Claude analyzes the changes to create an appropriate commit message:

  1. Identifies which files have been changed/added
  2. Determines the nature of changes (feature, bugfix, etc.)
  3. Evaluates the impact on the codebase
  4. Reviews for sensitive information
  5. Drafts a concise, descriptive commit message
  6. Ensures the message follows project conventions

Phase 3: Execution

Claude then executes the commit process:

{
  "description": "Perform git commit",
  "invocations": [
    {
      "tool_name": "Bash",
      "input": { 
        "command": "git add <relevant files>"
      }
    },
    {
      "tool_name": "Bash",
      "input": { 
        "command": "git commit -m \"$(cat <<'EOF'\n<commit message>\n\n🤖 Generated with Claude Code\n\nCo-Authored-By: Claude <noreply@anthropic.com>\nEOF\n)\"" 
      }
    }
  ]
}

The commit message is always passed via a HEREDOC to ensure proper formatting.

Phase 4: Verification

Claude verifies the commit succeeded:

{
  "tool_name": "Bash",
  "input": { "command": "git status" }
}

If the commit fails due to pre-commit hooks, Claude tries once more to include the automated changes.

Pull Request Workflow

Structure and Process

The pull request workflow follows a similar four-phase approach:

  1. Branch Analysis: Gathering branch state and history
  2. PR Content Creation: Drafting PR description and summary
  3. Submission: Creating the PR via GitHub CLI
  4. Confirmation: Verifying PR creation

Phase 1: Branch Analysis

Claude analyzes the branch state:

{
  "description": "Analyze branch for PR",
  "invocations": [
    {
      "tool_name": "Bash",
      "input": { "command": "git status" }
    },
    {
      "tool_name": "Bash",
      "input": { "command": "git diff" }
    },
    {
      "tool_name": "Bash",
      "input": { "command": "git log main...HEAD" }
    },
    {
      "tool_name": "Bash",
      "input": { "command": "git diff main...HEAD" }
    }
  ]
}

These commands provide a comprehensive view of:

  • Current uncommitted changes
  • Commit history since diverging from main
  • All changes that will be included in the PR

Phase 2: PR Content Creation

Based on the analysis, Claude:

  1. Lists all commits in the PR
  2. Summarizes the nature of changes
  3. Identifies the purpose/motivation
  4. Assesses impact on the project
  5. Drafts a concise PR summary
  6. Creates a basic test plan

Phase 3: Submission

Claude submits the PR using GitHub CLI:

{
  "tool_name": "Bash",
  "input": { 
    "command": "gh pr create --title \"<title>\" --body \"$(cat <<'EOF'\n## Summary\n<summary points>\n\n## Test plan\n<test steps>\n\n🤖 Generated with Claude Code\nEOF\n)\"" 
  }
}

If necessary, Claude first:

  • Creates a new branch if needed
  • Pushes to remote with -u flag if needed

Phase 4: Confirmation

Claude confirms the PR was created and returns the URL to the user.

Other Git Workflows

Viewing Pull Request Comments

{
  "tool_name": "Bash",
  "input": { "command": "gh api repos/owner/repo/pulls/123/comments" }
}

Addressing Review Feedback

For addressing PR feedback, Claude follows a workflow of:

  1. Reading feedback comments
  2. Making necessary changes
  3. Committing the changes
  4. Pushing updates to the PR

Key Features of Git Workflows

1. Template-Based Consistency

All commits and PRs follow standard templates:

Commit Template:

<Descriptive message>

🤖 Generated with Claude Code

Co-Authored-By: Claude <noreply@anthropic.com>

PR Template:

## Summary
<1-3 bullet points>

## Test plan
<Test steps/scenarios>

🤖 Generated with Claude Code

2. History-Aware Messaging

Commit messages and PR descriptions match the existing style in the repository by:

  1. Analyzing recent commit messages
  2. Following project conventions
  3. Maintaining consistent terminology

3. Selective Staging

Claude carefully selects which files to stage:

  • Only includes files relevant to the current task
  • Avoids sweeping commands like git add .
  • Stages related files together for coherent commits

4. Safety Guardrails

The workflows include multiple safety features:

  1. No Git Config Changes: Claude never modifies git configuration
  2. No Interactive Commands: Claude avoids commands requiring user input
  3. No Remote Pushing: Claude won't push to remote unless explicitly requested
  4. Empty Commit Prevention: Claude won't create empty commits

Best Practices Implemented

The git workflows enforce several best practices:

  1. Atomic Commits: Each commit represents a single logical change
  2. Descriptive Messages: Commits explain why changes were made, not just what
  3. Consistent Formatting: Standardized formatting for all git artifacts
  4. Appropriate Scope: Changes grouped by feature/component
  5. Pre-commit Verification: Changes reviewed before committing

End-to-End Git Workflow Example

  1. User asks Claude to "commit the changes we made to the authentication system"
  2. Claude gathers git information to understand the state of the repository
  3. Claude analyzes changes to identify authentication-related modifications
  4. Claude drafts an appropriate commit message based on the nature of changes
  5. Claude selectively stages the relevant files
  6. Claude creates the commit with the formatted message
  7. Claude verifies the commit succeeded
  8. Claude reports the commit status to the user

Through these structured git workflows, Claude ensures version control operations are performed consistently, following best practices, and with appropriate context awareness.