Claude Code Git Automation: From Diff to Draft PR
Automate the paperwork, keep the review
The quickest useful Claude Code workflow starts with changes you already understand: stage a focused diff, let Claude write the commit, then generate a draft pull request. Claude Code supports both non-interactive commands and PR creation, so you can automate repetitive Git work without handing over merge decisions. ([code.claude.com](https://code.claude.com/docs/en/headless))
1. Prepare your repository and GitHub access
This guide assumes a Bash-compatible shell, Git, Claude Code, GitHub CLI, and an existing GitHub repository with an origin remote. Run commands from the repository root. Authenticate GitHub CLI first:
gh auth login
Follow the prompts, including configuring Git authentication when offered. Then verify your session with gh auth status. Your model API credential and GitHub credential serve different purposes; you need both. ([cli.github.com](https://cli.github.com/manual/gh_auth_login))
Start from a clean working tree, update your base branch using your team's normal process, and create a dedicated branch:
git switch -c chore/refresh-docs
2. Connect Claude Code through 59API
59API offers low-cost, pay-as-you-go Claude and GPT access, with compatibility for Claude Code, Codex, and OpenAI SDK clients. It advertises official-quality model capability without downgrading and offers a referral rebate. For occasional commit and PR tasks, usage-based billing makes it worth comparing against a recurring subscription. Check current model prices rather than assuming every model costs the same. ([59api.com](https://59api.com/?lang=en&utm_source=openai))
Load your relay key into RELAY_API_KEY through your preferred secret manager, then run these shell commands:
export ANTHROPIC_BASE_URL="https://api.59api.com"
export ANTHROPIC_AUTH_TOKEN="$RELAY_API_KEY"
These are the endpoint and authentication variables shown in 59API's Claude Code setup. Keep credentials outside tracked repository files, and confirm your organization's policy permits sending source code through a third-party relay. ([59api.com](https://59api.com/?lang=en&utm_source=openai))
3. Stage only the changes you intend to commit
Make your documentation change, run the relevant checks, then select the exact hunks:
git add -p
Review the result with git diff --cached. Interactive staging lets you separate unrelated edits instead of sweeping everything into one commit. New, untracked files need explicit staging, such as git add docs/setup.md. Check for secrets, generated artifacts, and accidental deletions before proceeding. ([git-scm.com](https://git-scm.com/docs/git-add))
4. Let Claude create the commit
Run this single-line command after reviewing your staged diff:
claude -p "Review the staged diff and recent commit style. Commit only the staged changes with an accurate message. Do not stage files, amend commits, or push. If nothing is staged, stop." --allowedTools "Bash(git diff *),Bash(git log *),Bash(git status *),Bash(git commit *)"
The -p flag runs without an interactive conversation. The permission rules pre-approve the listed command patterns; the space before each wildcard matters. These rules are not a replacement for isolation or an audit of existing permissions. ([code.claude.com](https://code.claude.com/docs/en/headless))
Verify the actual result with git log -1 --stat. Do not treat a confident summary as proof that the intended files were committed.
5. Push, then automate a draft PR
Keep publishing explicit on your first run:
git push -u origin chore/refresh-docs
Then ask Claude to inspect the branch and open the PR:
claude -p "Inspect git log origin/main..HEAD and git diff origin/main...HEAD. Create a draft PR using gh pr create --draft --base main --head chore/refresh-docs with an explicit title and body. Explain the changes and risks. State that tests were not independently verified. Do not merge." --allowedTools "Bash(git log *),Bash(git diff *),Bash(gh pr create *)"
Replace main and the branch name when necessary. Explicit title and body values avoid GitHub CLI's corresponding prompts; --draft keeps the PR in draft status. Review the generated description and add the checks you actually ran. ([cli.github.com](https://cli.github.com/manual/gh_pr_create))
6. Make repeat runs predictable
- Bound execution: add --max-turns 8 and --max-budget-usd 1 to your Claude commands. The budget uses estimated API costs, not a guaranteed relay billing ceiling. ([code.claude.com](https://code.claude.com/docs/en/cli-reference))
- Gate publishing: in a wrapper script, stop on failed checks and verify a new commit exists before pushing.
- Prevent duplicates: check for an existing PR before creating another; never add automatic merging as a default.
If this workflow fits your routine, consider signing up for 59API and testing one small, reviewed change before scaling up.