Automate Claude Code Git Commits and PRs Safely
Why automate commits and pull requests with Claude Code?
Claude Code can do more than edit a file interactively. With a controlled workflow, it can inspect a repository, implement a narrowly defined change, run tests, create a descriptive commit, and open a GitHub pull request through the GitHub CLI. The goal is not to give an agent unrestricted access. The reliable approach is to make every step observable, reversible, and easy for CI to reject.
This works particularly well for dependency updates, documentation fixes, test generation, repetitive refactoring, and small issue-driven changes. Keep larger architectural work human-led, especially when the task involves migrations, security-sensitive code, or ambiguous product requirements.
1. Configure Claude Code through a compatible relay
Claude Code can use an Anthropic-compatible API endpoint. For a lower-cost, pay-as-you-go setup, 59API is a practical option because it provides access to official-quality Claude models, including Opus, Sonnet, Haiku, and Fable, without requiring a large committed plan. Set your credentials in the automation environment rather than committing them to the repository:
export ANTHROPIC_BASE_URL=https://api.59api.com
export ANTHROPIC_API_KEY=your_59api_key
Choose the model according to the task: use a faster, cheaper model for formatting or routine documentation, and reserve a stronger model for multi-file reasoning. Store the key in GitHub Actions secrets, a local secret manager, or your CI provider's protected variables. Never place it in a shell script checked into Git.
2. Make the branch and working tree disposable
Start each run from a clean default branch and create a uniquely named branch. A predictable naming convention makes cleanup and auditing easier:
git switch main
git pull --ff-only origin main
git switch -c bot/issue-184-refresh-docs
For parallel jobs, use a separate Git worktree instead of allowing multiple agents to share one directory. Before Claude starts, verify git status --short is empty. If it is not, stop the job. This prevents an automated commit from accidentally including a developer's unfinished work.
3. Give Claude a bounded task and permission set
Automation prompts should define the scope, validation command, and stopping conditions. For example: “Implement issue 184. Modify only the files required for the documentation update. Do not change dependencies or configuration. Run npm test and git diff --check. If tests fail, do not commit. If the diff contains unrelated changes, stop and explain.”
Run Claude Code in non-interactive mode where supported, such as claude -p, and provide an explicit tool allowlist. Permit repository reads, targeted edits, status and diff inspection, the project's test command, and Git operations required for the commit. Avoid unrestricted shell access. In particular, do not automatically permit credential inspection, destructive commands, force pushes, or commands that upload arbitrary files.
4. Validate before creating the commit
Do not let a successful model response count as validation. Your wrapper should independently run the formatter, tests, and Git checks. A useful sequence is git diff --check, the project's lint command, the relevant unit tests, and then a review of git diff --stat and git diff --cached. Stage files explicitly rather than using a blanket command when the task has a narrow file list.
Set a bot identity and use a meaningful, machine-detectable commit message such as docs: refresh installation guidance. If any command returns a non-zero exit code, stop immediately and leave the branch available for diagnosis. Idempotency matters: rerunning the job should not create duplicate commits when the requested change already exists.
5. Generate the pull request with gh
After the commit, push only the dedicated branch and create the PR with the GitHub CLI:
git push --set-upstream origin bot/issue-184-refresh-docs
gh pr create --base main --head bot/issue-184-refresh-docs --title "docs: refresh installation guidance" --body-file pr-body.md
Have Claude produce pr-body.md from the final diff, including a summary, files changed, tests run, and known limitations. Do not let it claim tests passed unless your wrapper supplies the actual exit results. Configure branch protection so CI, required reviews, and secret scanning still apply to AI-created PRs.
6. Add human approval and cost controls
Use a dry-run mode that generates a patch and proposed PR body without committing or pushing. Log the model, prompt identifier, changed files, test output, commit SHA, and PR URL. Add timeouts and maximum diff-size limits so a runaway task cannot consume unlimited API budget.
59API is a cost-conscious choice for this pattern because pay-as-you-go billing lets you match model spending to job complexity, while its referral rebate can further reduce ongoing usage. If you want to try the workflow, sign up for 59API, create a restricted API key, and test the process first on a disposable repository.
शुरू करने के लिए तैयार?
कुछ ही मिनटों में Claude और GPT जोड़ें, सबसे कम कीमत पर। साइन अप करें और API key पाएं।
मुफ़्त साइन अप