DEV Community

brian austin
brian austin

Posted on

Claude Code git workflow: auto-commit, branch-per-feature, and /git slash command

Claude Code git workflow: auto-commit, branch-per-feature, and /git slash command

If you're using Claude Code without a git workflow, you're missing one of the biggest productivity multipliers. Here's how to set up automatic commits, branch isolation, and a custom /git slash command that handles your entire git lifecycle.

The problem with default Claude Code git usage

By default, Claude Code doesn't touch git. It edits files, runs tests, fixes bugs — but you're manually committing everything. This creates two problems:

  1. No rollback points — if Claude makes a bad change across 10 files, you're git diff-ing to figure out what happened
  2. No context isolation — Claude's changes mix with your manual edits, making the history unreadable

The fix is a structured git workflow baked into your CLAUDE.md.

Step 1: Add git discipline to CLAUDE.md

In your project root CLAUDE.md:

## Git Workflow

- Create a new branch before starting any task: `git checkout -b claude/task-name`
- Commit after every logical unit of work (not just at the end)
- Use conventional commits: `feat:`, `fix:`, `refactor:`, `test:`
- Never commit to main directly
- Run tests before committing if a test suite exists
Enter fullscreen mode Exit fullscreen mode

This gives Claude standing instructions that apply to every session.

Step 2: Create a /git slash command

Create .claude/commands/git.md:

Git workflow helper. Run these steps:

1. Check current status: `git status` and `git log --oneline -5`
2. If on main, create a new branch: `git checkout -b claude/$(date +%Y%m%d-%H%M%S)`
3. Stage all changes: `git add -A`
4. Write a conventional commit message summarizing what changed
5. Commit: `git commit -m "<message>"`
6. Show the commit: `git log --oneline -1`

Do this now.
Enter fullscreen mode Exit fullscreen mode

Now you can type /git at any point in a Claude Code session and it will commit the current state with a meaningful message.

Step 3: Branch-per-task with auto-naming

For bigger tasks, create .claude/commands/start-task.md:

Start a new task. Arguments: $ARGUMENTS

1. Ensure working tree is clean: `git status`
2. Pull latest main: `git checkout main && git pull`
3. Create a task branch: `git checkout -b claude/$ARGUMENTS`
4. Confirm the branch: `git branch --show-current`
5. Report ready to start
Enter fullscreen mode Exit fullscreen mode

Usage:

/start-task add-user-auth
Enter fullscreen mode Exit fullscreen mode

This creates claude/add-user-auth and you're isolated from main immediately.

Step 4: Commit hooks via Claude Code hooks

In your .claude/settings.json:

{
  "hooks": {
    "PostToolUse": [
      {
        "matcher": "Write|Edit|MultiEdit",
        "hooks": [
          {
            "type": "command",
            "command": "echo 'File modified: run /git when ready to commit'"
          }
        ]
      }
    ]
  }
}
Enter fullscreen mode Exit fullscreen mode

This reminds you to commit after every file write. You can make it auto-commit by replacing the echo with a git add+commit, but I prefer the reminder so I review what's staged.

Step 5: The /checkpoint slash command

Create .claude/commands/checkpoint.md:

Create a checkpoint commit (work in progress, not for review).

1. Stage all: `git add -A`
2. Commit with WIP prefix: `git commit -m "wip: checkpoint $(date +%H:%M)"`
3. Show status

This is a safety snapshot, not a final commit.
Enter fullscreen mode Exit fullscreen mode

Use /checkpoint before doing anything risky — refactoring a module, changing an API, updating dependencies. If Claude goes sideways, git reset HEAD~1 puts you back.

The full workflow in practice

# Start session
/start-task refactor-auth-module

# Claude does work...
# Before risky change:
/checkpoint

# Claude does more work...
# After logical unit complete:
/git

# End of session:
/git
git push origin claude/refactor-auth-module
Enter fullscreen mode Exit fullscreen mode

Your git history now looks like:

feat: add JWT refresh token rotation
test: add coverage for token expiry edge cases
refactor: extract auth middleware to separate module
wip: checkpoint 14:32
fix: handle null user case in session validation
Enter fullscreen mode Exit fullscreen mode

Readable, rollback-able, and PR-ready.

Rate limits and long sessions

One issue with long Claude Code sessions: you hit rate limits mid-task, right when you're in the middle of a branch. If you've been making /checkpoint commits, you can pick up where you left off after the limit resets.

If you're on the direct Anthropic API, you'll see "overloaded" errors on heavy usage days. Setting ANTHROPIC_BASE_URL to a proxy removes this — the proxy distributes load so your session doesn't get cut off mid-branch.

export ANTHROPIC_BASE_URL=https://simplylouie.com/api/proxy
Enter fullscreen mode Exit fullscreen mode

SimplyLouie runs Claude behind the scenes at ✌️2/month — the same model, no rate limit interruptions, so your git workflow sessions don't get cut off at the worst moment.

Summary

Command What it does
/start-task <name> Create branch, pull latest
/checkpoint WIP commit for rollback safety
/git Proper conventional commit
PostToolUse hook Reminds you to commit after writes

The pattern: branch → work → checkpoint → work → commit → push. Claude handles the git mechanics, you review the result.

What's your current git setup with Claude Code? Curious if others have automated the PR creation step too.

Top comments (0)