Git Workflows with Claude Code: Branching, PRs, and Commits
Claude Code has native git integration — it reads your repository history, creates branches, makes commits, and writes PR descriptions without leaving your terminal session. The highest-value use cases are commit message generation (saves 2–5 minutes per commit), PR description writing (saves 20–30 minutes per PR), and merge conflict resolution (reduces a painful manual process to guided resolution).
This guide covers the complete git workflow with Claude Code, from daily commits to complex rebases.
Setup: Claude Code and git permissions
Claude Code can run git commands by default. Before your first session, verify it has the right context:
In your CLAUDE.md:
## Git conventions
- Branch naming: feature/description, fix/description, chore/description
- Commit format: conventional commits (feat:, fix:, chore:, docs:, refactor:)
- PR template: [link to .github/pull_request_template.md]
- Default branch: main
- Protected branches: main, staging (no direct push)
This ensures every Claude Code session follows your team's git conventions without re-explaining them.
Daily commit workflow
The most common use: you've been coding for an hour, you have a set of changes, and you need a good commit message.
Simple approach:
You: Create a commit for the changes I just made.
Claude Code will:
- Run
git diff --staged(orgit diff HEADif nothing is staged) - Read the changes
- Stage appropriate files (if you haven't)
- Write a conventional commit message describing what changed and why
- Ask for approval before committing
With more context:
You: I've implemented the rate limiter and its tests.
Commit this with a descriptive message following conventional commits format.
For complex multi-part changes:
You: I've made changes to three areas: the auth middleware,
the user service, and the tests. Should these be one commit or three?
Claude Code will assess the changes and recommend whether to squash or split, then execute your decision.
Branch creation and naming
You: Create a new branch for the Stripe payment integration feature.
You: I'm starting work on fixing the race condition in the job queue.
Create an appropriate branch name and check it out.
Claude Code generates branch names following your CLAUDE.md conventions. For the second example, it might create fix/job-queue-race-condition.
For feature branches with issue numbers:
You: Create a branch for GitHub issue #342 — the login timeout bug.
Output: git checkout -b fix/342-login-timeout
Reading git history for context
Claude Code can search git history to answer questions that accelerate debugging:
You: When was the auth middleware last changed and what changed?
You: Who introduced the duplicate check in user-service.ts and why?
(check git log and git blame)
You: What commits touched the payment module in the last 30 days?
This is particularly useful when joining a project or debugging regressions — Claude Code reads the commit history and gives you a narrative of what changed and when.
Merge conflict resolution
Merge conflicts are a painful manual process. Claude Code turns them into a guided resolution:
You: I have merge conflicts in three files. Help me resolve them.
Claude Code will:
- Run
git diff --mergeto see all conflict markers - For each conflict, explain what both sides are trying to do
- Propose a resolution (keeping one side, merging both, or a custom merge)
- Implement the resolution after your approval
- Mark the file as resolved and continue
For complex conflicts:
You: The conflict in api/payments.ts is between my rate limiter
and the upstream async refactor. Resolve it preserving both changes.
Claude Code reasons about the intent of both changes and produces a merged version that satisfies both.
PR description generation
Writing good PR descriptions is time-consuming. Claude Code automates this:
You: Generate a PR description for this branch.
Include: summary, what changed, how to test it.
Claude Code:
- Reads
git log main..HEADto see all commits in the branch - Reads
git diff main...HEADfor the full diff - Generates a structured PR description following your template
- Outputs it ready to paste into GitHub/GitLab
Example output structure:
## Summary
Adds rate limiting middleware to all authenticated API endpoints.
## Changes
- `middleware/rate-limiter.ts`: new Redis-backed rate limiter,
100 req/min per IP
- `app/api/[...route]/route.ts`: rate limiter applied to all routes
- `__tests__/rate-limiter.test.ts`: 12 new tests covering limit
enforcement and bypass resistance
## Testing
1. `npm test` — all tests pass
2. Manual: hit `/api/user/profile` 101 times in 60 seconds —
verify 429 response on request 101
## Notes
Redis connection uses existing `lib/redis.ts` client.
No new infrastructure required.
Interactive rebase assistance
Rebasing is one of the most error-prone git operations. Claude Code guides you through it:
You: I want to squash my last 5 commits into one clean commit
before merging to main.
You: Help me rebase my feature branch onto main,
resolving any conflicts that come up.
You: Clean up my commit history — there are several
"WIP" commits that should be squashed.
Claude Code prepares the rebase command, handles conflicts as they arise, and explains what it's doing at each step. You stay in control; Claude Code does the mechanical work.
Git stash management
You: I need to switch branches but I have uncommitted work.
Stash it with a descriptive name.
You: List my stashes and apply the one from the payment feature work.
You: I have 8 old stashes. Clean up any that are more than 2 weeks old.
Stash names make it easy to resume context when you return:
stash@{0}: WIP on feature/rate-limiter: rate limiter middleware, Redis key structure unfinished
Undoing changes safely
Claude Code helps with the often-confusing range of git undo operations:
You: I want to undo my last commit but keep the changes staged.
(git reset --soft HEAD~1)
You: I accidentally committed a .env file. Remove it from git history.
(git filter-branch or BFG repo cleaner)
You: Revert this specific commit: abc1234, without losing later work.
(git revert abc1234)
Critically, Claude Code distinguishes between reversible and irreversible operations. For destructive git operations (reset --hard, branch -D, push --force), it will warn you and ask for explicit confirmation before executing.
Using worktrees for parallel branches
For working on multiple features simultaneously without branch switching:
You: Set up worktrees so I can work on the payment feature and
the bug fix simultaneously without switching branches.
Claude Code creates the worktrees:
git worktree add ../myapp-payment-feature feature/payment-integration
git worktree add ../myapp-login-fix fix/login-timeout
Now you can open two Claude Code sessions in parallel — one in each worktree. See the worktree guide for the full parallel development workflow.
Common git patterns in CLAUDE.md
Add these to your CLAUDE.md to make every session git-aware:
## Git conventions
- Branch naming: feature/description, fix/description, chore/description
- Commit format: conventional commits (feat:, fix:, chore:, docs:, refactor:, test:)
- Commit scope: feat(auth):, fix(api):, etc.
- Never commit directly to main or staging
- Always create a branch before starting work
- PR title format: "[JIRA-123] Brief description"
- Squash feature commits before merge
## Git safety rules
- Never use --force on shared branches
- Always verify diff before committing
- Stage specific files, not git add -A
- Run tests before creating a commit
Frequently asked questions
Can Claude Code push to remote branches?
Yes, with your permission. When you ask Claude Code to "push this branch" or "open a PR", it will run git push and (if you have the gh CLI installed) create the PR. It will show you the command and ask for confirmation before executing pushes.
Will Claude Code accidentally commit sensitive files?
Claude Code stages specific files rather than git add -A by default. It also reads your .gitignore. For extra safety, add your sensitive file patterns to your CLAUDE.md: "Never stage or commit files matching: .env, *.pem, secrets/".
Can Claude Code resolve all merge conflicts automatically? For straightforward conflicts (one side added, one side deleted, or both added independent changes), yes. For conflicts where both sides modify the same logic in incompatible ways, Claude Code will propose options and ask you to choose. It won't make an arbitrary resolution without your input.
Does Claude Code work with GitLab and Bitbucket?
Claude Code uses standard git commands, so it works with any git remote. GitHub-specific features (PR creation, issue linking) require the gh CLI. For GitLab, the glab CLI provides similar functionality — tell Claude Code in your CLAUDE.md which CLI you're using.
For a full reference on Claude Code capabilities beyond git — including CLAUDE.md configuration, hooks, MCP servers, and advanced workflows — see the Claude Code Complete Guide.
Take It Further
Claude Code Power Prompts 300 — 300 ready-to-use prompts organized by workflow, including a complete git workflow section with prompts for every scenario covered in this guide.
→ Get Claude Code Power Prompts — $29
30-day money-back guarantee. Instant download.