Claude Code git integration: auto-commit, branch per task, never lose work again
If you've ever lost a Claude Code session mid-task and had no idea what it changed, this post is for you.
Claude Code doesn't have a native undo button. Git is your undo button.
Here's how I set up Claude Code to work with git so every session is recoverable, every task is isolated, and you never have to manually commit again.
The problem: Claude Code changes files silently
You ask Claude Code to refactor a module. It touches 12 files. Some changes are great. Two are wrong. You hit Ctrl+C to stop it.
Now what?
git diff shows a wall of changes. You can't tell which parts to keep.
The fix: commit before every Claude Code session. Branch per task.
Step 1: Always start Claude Code on a clean branch
# Before every Claude Code session:
git checkout -b claude/$(date +%Y%m%d-%H%M)-task-name
This creates a branch like claude/20260403-1430-refactor-auth.
Now everything Claude does is isolated. If it goes wrong:
git checkout main
git branch -D claude/20260403-1430-refactor-auth
Clean. Gone. Back to where you started.
Step 2: Use CLAUDE.md to enforce the branching pattern
Add this to your CLAUDE.md:
## Git workflow
Before making any changes:
1. Check `git status` - confirm we're on a feature branch, not main
2. If on main, create a branch: `git checkout -b claude/[task-name]`
3. Commit after each logical chunk of work
4. Never force-push
5. Never commit directly to main
Now Claude Code will check the branch before touching anything.
Step 3: Auto-commit with hooks
Claude Code's PostToolUse hooks can trigger commits automatically after file writes.
Add to your .claude/settings.json:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit|MultiEdit",
"hooks": [
{
"type": "command",
"command": "git add -A && git diff --cached --quiet || git commit -m 'claude: auto-save'"
}
]
}
]
}
}
This runs after every file write. If there are staged changes, it commits them with claude: auto-save.
You end up with a granular git history of every change Claude made:
claude: auto-save
claude: auto-save
claude: auto-save
Not pretty, but recoverable. You can always squash later.
Step 4: Name your commits with Claude's context
The auto-commit message is boring. Make it descriptive:
{
"hooks": {
"PostToolUse": [
{
"matcher": "Write|Edit",
"hooks": [
{
"type": "command",
"command": "git add -A && git diff --cached --quiet || git commit -m 'claude: $(git diff --cached --name-only | head -3 | tr '\n' ' ')'"
}
]
}
]
}
}
Now commits look like:
claude: src/auth.js src/middleware.js
claude: tests/auth.test.js
You can see exactly which files changed in each step.
Step 5: Add a /checkpoint slash command
Create .claude/commands/checkpoint.md:
Create a git checkpoint with a descriptive message.
Run:
1. `git status` to see current state
2. `git add -A`
3. `git commit -m "checkpoint: $ARGUMENTS"`
4. Confirm the commit was created
If there's nothing to commit, say so.
Now you can type /checkpoint before refactoring auth in Claude Code and it commits the current state with your message.
Useful before risky operations.
Step 6: Review Claude's work before merging
At the end of a Claude Code session, review what it did:
# See all commits Claude made
git log --oneline main..HEAD
# See all changed files
git diff main --name-only
# Interactive review of changes
git diff main
If everything looks good:
git checkout main
git merge --squash claude/20260403-1430-refactor-auth
git commit -m "refactor: auth module (via Claude Code)"
Clean history. All of Claude's auto-commits squashed into one meaningful commit.
The full workflow
# 1. Start session
git checkout -b claude/$(date +%Y%m%d-%H%M)-feature-name
# 2. Do Claude Code work
# (auto-commits happen via hooks)
# 3. Review at end
git log --oneline main..HEAD
git diff main
# 4. Merge if happy
git checkout main
git merge --squash claude/...
git commit -m "feat: what Claude built"
# 5. Clean up
git branch -D claude/...
That's it. Every Claude Code session is a recoverable, reviewable, clean git branch.
Rate limits break the workflow
The one thing that breaks this workflow: Claude Code hitting rate limits mid-task.
You're in the middle of a refactor, Claude stops with overloaded_error, and you have half-finished changes across 6 files.
The fix I use: set ANTHROPIC_BASE_URL to point to a proxy that removes rate limits.
export ANTHROPIC_BASE_URL=https://simplylouie.com/api/claude
SimplyLouie costs $2/month and removes the rate limit interruptions. Claude Code works the same - same model, same API - but without the mid-session stops.
Combined with the branching workflow above, you get: isolated branches + auto-commits + no rate limit interruptions = fully recoverable Claude Code sessions.
Summary
| Problem | Solution |
|---|---|
| Can't undo Claude changes | Branch per task |
| Lost context after Ctrl+C | Auto-commit hooks |
| Don't know what Claude changed | git diff main |
| Mid-session rate limits |
ANTHROPIC_BASE_URL proxy |
| Messy git history | Squash merge at end |
Git is already the best undo button for code. Make Claude Code use it.
Top comments (0)