Git Workflows & Worktrees
After this lesson, you will be able to:
- Use Claude Code for the full git lifecycle (branches, commits, PRs, releases) with conventional-commit messages and proper safety
- Apply git worktrees to run parallel Claude Code sessions on different feature branches without merge conflicts
- Configure CLAUDE.md and settings.json to prevent the four classic git incidents (force-push to main, lost commits, accidental rm of .git, broken history)
- Pick the right pattern (main session, fork, or worktree-isolated agent) for any git workflow
Before you start
#The Claude Code Git Cycle
1. plan → CLAUDE.md + Plan mode
2. branch → git checkout -b feat/X
3. implement → Claude edits files, runs tests
4. commit → Conventional commit message via Claude
5. push → git push -u origin feat/X
6. PR → gh pr create with body via Claude
7. review → /review or /ultrareview
8. merge → via GitHub/GitLab UI (or gh pr merge)
Claude Code handles steps 1-7 well. Step 8 is intentional, humans approve merges to main.
#Conventional Commit Messages
Claude Code commonly writes conventional commits, and follows whatever your CLAUDE.md says:
feat(auth): add OAuth login via Google
fix(api): handle null user in /me endpoint
docs(readme): clarify setup instructions
refactor(db): extract connection pooling helper
test(auth): add edge case for expired tokens
chore(deps): bump react to 19.0.4
<type>(<scope>): <description>. Types: feat, fix, docs, refactor, test, chore, perf, style, build, ci.If your project uses different conventions, document them in CLAUDE.md:
## Commit Conventions
- Use conventional commits: feat:, fix:, docs:, refactor:, test:, chore:
- Scope is the affected module (e.g., feat(auth), fix(api/users))
- Subject in lowercase, imperative ("add", not "added" or "adds")
- Body explains WHY, not WHAT (the diff shows what)
Claude will read this and produce matching commits.
#Worktrees: Parallel Branch Work
A git worktree is a second working directory tied to your main repo, but checked out to a different branch.
# Main repo at ~/projects/myapp on branch main
cd ~/projects/myapp
git worktree add ../myapp-feat-auth feat/auth-rewrite
git worktree add ../myapp-feat-billing feat/billing-redesign
# Now you have three working directories:
ls ~/projects/
# myapp/ ← main branch
# myapp-feat-auth/ ← feat/auth-rewrite branch
# myapp-feat-billing/ ← feat/billing-redesign branch
Open three Claude Code sessions, one per directory. They work in parallel without conflict.
#When to Use Worktrees
| Scenario | Use Worktree? |
|---|---|
| Reviewing a PR while continuing main work | ✅, checkout the PR in a worktree, review it, switch back |
| Running parallel agentic refactors on independent features | ✅, each Claude session in its own worktree |
Hot-fix on main while feature branch in progress | ✅, main stays clean in main worktree |
| Quick context switch (5 min) | ❌, git stash is faster |
| Single feature, single session | ❌, overhead not worth it |
#Cleaning Up Worktrees
# After PR merged, remove the worktree
git worktree remove ../myapp-feat-auth
# Lists current worktrees
git worktree list
Worktrees can leave orphan branches if not cleaned up. Periodically:
git worktree prune # removes stale worktree references
#Safety: Preventing Git Incidents
.claude/settings.json:{
"permissions": {
"deny": [
// Force-push to protected branches
"Bash(git push --force * main)",
"Bash(git push --force * master)",
"Bash(git push --force-with-lease * main)",
"Bash(git push -f * main)",
// Destructive history rewrites
"Bash(git reset --hard origin/main)",
"Bash(git rebase -i *)", // interactive rebase requires a human
// Removing git internals
"Bash(rm -rf .git*)",
// Discarding all local changes
"Bash(git checkout .)",
"Bash(git restore .)",
"Bash(git clean -fd)"
],
"allow": [
// Safe read operations
"Bash(git status)",
"Bash(git diff *)",
"Bash(git log *)",
"Bash(git branch *)",
"Bash(git fetch *)",
"Bash(git pull --rebase)",
// Safe write operations
"Bash(git add *)",
"Bash(git commit *)",
"Bash(git push -u origin feat/*)", // feature branches only
"Bash(git checkout -b *)",
"Bash(git merge --no-ff *)"
]
}
}
#PR Creation Workflow
# Stage and commit
git add src/auth/ tests/auth/
git commit -m "feat(auth): add OAuth login via Google
- Add GoogleAuthProvider wrapper
- Add /api/auth/callback/google endpoint
- Add E2E test in playwright/auth.spec.ts
Closes #142"
# Push
git push -u origin feat/auth-google
# Create PR
gh pr create --title "feat(auth): add OAuth login via Google" --body "$(cat <<'EOF'
## Summary
- New OAuth provider for Google
- Updates the auth flow to support multiple providers
- Closes #142
## Test plan
- [x] Manual: log in with Google account
- [x] E2E test passes (playwright/auth.spec.ts)
- [x] Existing email/password auth still works
EOF
)"
Claude Code will follow this pattern when you ask it to "open a PR", especially if your CLAUDE.md describes your PR template. You still read the title and body before you submit.
#/git Slash Command Patterns
> /git commit # claude analyzes diff, writes commit message
> /git pr # claude opens PR with summary
> /git review # claude reviews uncommitted changes
> /git status # claude summarizes git status
/git command. These are examples of custom slash commands you would define yourself in .claude/commands/ or .claude/skills/; see the Slash Commands & Skills lesson.)#Key Takeaways
- Conventional commits by default, Claude writes them well; document your project's variations in CLAUDE.md
- Worktrees enable parallel agentic work. Three Claude sessions on three branches without conflict
- Deny force-push to main in settings.json and turn on branch protection on the server. A git incident that loses work is preventable
gh pr createwith HEREDOC body: Claude generates good PR descriptions- Worktree cleanup.
git worktree removeafter PR merges;git worktree prunequarterly
#Quick Check
You want Claude to do a hot-fix on main while continuing your feature work in `feat/billing`. Best approach?
You want Claude to never, under any circumstances, force-push to main from any session anyone runs. Where do you put the rule?