headroom.walls.sh · git

Git + Claude Code

Claude Code integrates naturally into git workflows — writing commit messages from staged diffs, resolving merge conflicts with full context, generating PR descriptions, and reviewing changes before push. This page covers the patterns that save time every day.

Commit messages from staged diffs

The most common Claude Code git task: stage your changes, then ask for a commit message that reflects what actually changed.

git add -p   # stage what you want
git diff --staged | claude --print "write a conventional commit message for this diff — one subject line under 72 chars, then a body paragraph explaining why"

Or from inside a Claude Code session after making changes:

claude "look at the staged diff and write a commit message. Use conventional commits format: feat/fix/refactor/docs. Focus on why, not what."

Claude Code reads the diff, understands what the change does, and writes a message that would make sense to a reviewer six months later — not just "update auth.js".

Ask for "why, not what" explicitly. Claude Code can always read what changed from the diff. The why — the motivation, the constraint, the bug being fixed — is what commit messages are actually for.

Code review before pushing

Review your own changes before they go up for PR:

git diff main..HEAD | claude --print "review this diff for: bugs, edge cases not handled, missing error handling, anything a reviewer would flag"

Or from inside a session with full project context:

claude "review the changes on this branch compared to main. Look for: logic errors, unhandled edge cases, missing tests, anything that would get a PR comment"

Claude Code can read the actual files — not just the diff — so it catches issues like "this change assumes the user is authenticated but the route middleware was removed three commits ago."

Merge conflict resolution

When you hit a merge conflict, Claude Code can resolve it with context from both sides:

claude "I have merge conflicts in src/api/users.js. Read the file, understand both sides of the conflict, and resolve it correctly — preserve the intent of both branches."

It reads the conflict markers (<<<<<<<, =======, >>>>>>>), understands what each side is trying to do, and produces a clean resolution. For complex conflicts across many files:

claude "there are merge conflicts across the src/models/ directory. Resolve each one — our branch added new fields, theirs refactored the base class. Preserve both."

PR descriptions

Generate a full PR description from the branch diff:

git log main..HEAD --oneline | claude --print "here are the commits on this branch. Write a GitHub PR description: summary paragraph, bullet-point changes, test plan section."

With the full diff for more detail:

git diff main..HEAD | claude --print "write a GitHub PR description for this diff. Include: what changed and why, how to test it, any migration steps needed."

Understanding git history

When you need to understand what changed in a range of commits:

git log --oneline -20 | claude --print "summarize what work happened in these commits — group by theme (feature, bug fix, refactor)"

Or dig into a specific commit:

git show abc1234 | claude --print "explain what this commit does and why it was necessary"

For tracking down when a bug was introduced:

claude "use git log and git show to find when the rate limiting logic in src/middleware/rateLimit.js was last changed, and what the change was"

Preparing a clean commit history before PR

If you have a messy branch and want Claude Code to help organize it:

claude "look at the commits on this branch with git log. Describe the logical groupings — what should be squashed, what should stay separate, what order makes the history readable"

Claude Code won't rewrite history for you (interactive rebase requires human input at each step), but it will give you the exact rebase plan to execute:

claude "give me the exact git rebase -i commands to squash these commits into three clean logical units: the schema change, the API change, and the tests"

Git hooks with Claude Code

Use Claude Code as a commit-msg hook to improve commit messages automatically:

# .git/hooks/commit-msg
#!/bin/sh
MSG=$(cat "$1")
if [ ${#MSG} -lt 20 ]; then
  echo "Commit message too short — run: claude 'write a commit message for the staged diff'"
  exit 1
fi

Or a pre-push hook that runs a quick review:

# .git/hooks/pre-push
#!/bin/sh
git diff origin/main..HEAD | claude --print "flag any obvious bugs or missing error handling in this diff. If none, say LGTM." --output-format text
Session budget note: git operations themselves are fast, but asking Claude Code to review large diffs or resolve many merge conflicts across dozens of files adds up. A thorough pre-push review of a large branch can be 10–20 tool calls.

Stash and branch workflows

Claude Code understands git state and can help navigate it:

claude "I'm in the middle of this feature but need to switch branches to fix a bug. Help me stash the right things and create a clean branch for the hotfix."
claude "I accidentally committed to main instead of a feature branch. Help me fix the git history — move these commits to a new branch and reset main."

Monitor usage during long git sessions

Headroom — know your session budget before a big merge

Resolving merge conflicts across many files, reviewing a large PR, or cleaning up a messy branch history can consume more Claude Code session budget than expected. Headroom shows your session (5h) and weekly (7d) utilization live in the macOS menu bar — no token, no API key, just the file Claude Code writes to ~/.claude/.

Install in one line:

brew install patwalls/tap/headroom

Color-coded from calm to amber to red. You'll know before you start a long conflict resolution whether you have the headroom to finish it.


Refactoring with Claude Code
Writing tests with Claude Code
Debugging with Claude Code
Claude Code hooks — PreToolUse, PostToolUse, statusLineHook