How to Use AI for Git (Commits, Branches, Conflicts)
AI can draft commit messages, suggest branch names and explain a conflict hunk. You still run git yourself. Never let an agent force-push or rewrite shared history without a clear OK.
In short: show the staged diff for messages, ask for conventional commits, use AI to explain conflict markers, and keep destructive git commands on manual approval.
Why AI helps with Git
Blank commit messages waste review time. Conflict markers are scary the first hundred times. AI drafts clear subjects and explains both sides of a hunk. You decide what to keep and you type the git commands you understand.
What AI can help with
- Commit messages: from
git diff --staged— subject + short body. - Branch names: short, kebab-case, tied to the ticket or intent.
- Explain a conflict: what each side changed; you still choose.
- PR summary: from the commit list or full PR diff.
How AI + Git flows
Write a conventional commit for this staged diff. One subject ≤72 chars (feat/fix/docs/refactor/test/chore). Then a 1–3 line body explaining why. No fluff. No secrets.
# After: git diff --staged # Prompt the AI: "Write a conventional commit message for this staged diff. One subject line under 72 chars, then a short body." feat(billing): use Decimal for GST totals Avoid float rounding surprises on prices like 99.99. Add pytest cases for 18%, zero rate, and negatives.
How to use AI with Git step by step
- Stage deliberately (
git add -p) so the message matches the change. - Paste
git diff --staged(or use Copilot / Cursor commit helpers if your plan has them). - Edit the subject if the AI is vague (“fix stuff” → real intent).
- On conflicts: paste one hunk with markers; ask “explain both sides; recommend a merge keeping X behaviour.” Then you edit the file.
- Re-run tests after resolving conflicts before you commit.
| Safe | Dangerous without explicit OK |
|---|---|
| Draft commit message / PR summary | git push --force to shared main |
| Suggest branch name | git reset --hard on uncommitted work you need |
| Explain conflict markers | git clean -fdx wiping local files |
| Summarise recent commits | Rewriting published history on a shared branch |
Settings / tool tips
- GitHub Copilot and Cursor can suggest commit messages from the staged diff (UI labels change — look for the commit message sparkle / generate action in the Source Control view).
- Claude Code CLI: you can ask it to draft a message from the staged diff; still run
git commityourself unless you knowingly allow it. - Project rules: “conventional commits; never commit .env; never force-push main.”
- Conflicts: resolve in the IDE merge UI; use AI only to explain, then test.
Quick quiz
1. What should you paste for a commit message?
The staged diff (git diff --staged), not unrelated files.
2. Who resolves merge conflicts?
You — AI can explain hunks; you edit and test.
3. Name one dangerous command to keep gated
force-push to shared main, hard reset, or clean -fdx.
4. What style of commit subject works well?
Conventional commits: feat/fix/docs/… plus a clear why.
Save tokens and money
One staged diff → one message. Do not paste entire repo history into chat to “write better commits.”