| name | git-master |
| description | Use when performing git operations - commits, rebasing, cherry-picking, history cleanup, or conflict resolution. Ensures atomic commits, clean history, and consistent commit message style. |
| allowed-tools | Read, Grep, Glob, Bash |
Git Master
Overview
Messy git history obscures intent, makes debugging harder, and wastes reviewer time. Clean history tells the story of WHY changes were made, not just WHAT changed.
Core principle: Every commit should be a single, complete, logical change that can be understood, reverted, or cherry-picked independently.
Violating the letter of this process is violating the spirit of clean version control.
The Iron Law
ONE LOGICAL CHANGE PER COMMIT. NO EXCEPTIONS.
If a commit does two things, it should be two commits. If a commit is incomplete, it should not exist yet.
No exceptions:
- "I'll split it later" → Split it now
- "It's a small extra change" → Separate commit
- "They're related" → Related is not identical
When to Use
Use for ANY git operation beyond trivial:
- Creating commits (single or multiple)
- Rebasing branches (interactive or standard)
- Cherry-picking commits across branches
- Cleaning up history (amend, squash, fixup)
- Resolving merge conflicts
- Preparing branches for review
Use this ESPECIALLY when:
- Repository has established commit conventions
- Branch has messy work-in-progress commits before PR
- Cherry-picking between release branches
- Resolving complex merge conflicts
- History needs cleanup before merging
Don't skip when:
- "It's just a quick fix" (quick fixes deserve clean commits too)
- "Nobody reads commit history" (future-you does, during
git bisect)
- "I'll squash on merge anyway" (squash merges lose valuable context)
When NOT to Use
- Initial prototyping — Experiment freely, clean up before PR
- Throwaway branches — No need for perfect history on disposable work
- Automated commits — CI/CD generated commits follow their own patterns
The Six Phases
Phase 1: Style Detection
BEFORE making any commit, understand the repo's conventions:
-
Analyze Recent History
git log --oneline -20
-
Detect Commit Message Pattern
| Pattern | Example | Convention |
|---|
| Conventional Commits | feat(auth): add OAuth2 flow | type(scope): description |
| Imperative mood | Add user validation | Verb-first, present tense |
| Ticket prefix | [PROJ-123] Fix login bug | [TICKET] description |
| Emoji prefix | :bug: Fix null pointer | Emoji + description |
| Freeform | Fixed the login thing | No consistent pattern |
-
Match the Detected Style
- Use the same pattern for your commits
- If no pattern detected, default to Conventional Commits
- Never mix styles within a PR
Completion criteria:
Phase 2: Atomic Commits
Every commit must be atomic — one logical change, complete and self-contained.
-
Stage Selectively
git add src/auth/login.ts src/auth/login.test.ts
git add -p src/utils/helpers.ts
Never use git add . or git add -A unless you have verified every change belongs together.
-
Verify Staged Content
git diff --cached
git diff
-
Write the Commit Message
Structure:
<type>(<scope>): <what changed> (max 72 chars)
<why this change was needed>
<any important context>
Refs: #issue-number (if applicable)
Good vs Bad:
| Bad | Good |
|---|
fix stuff | fix(auth): prevent session timeout on token refresh |
WIP | feat(cart): add quantity validation for checkout |
updates | refactor(api): extract retry logic into shared middleware |
fix tests | test(auth): cover edge case for expired JWT tokens |
-
Verify Atomicity
- Does this commit do exactly ONE thing?
- Could someone revert JUST this commit safely?
- Does the codebase compile/pass tests at this commit?
Completion criteria:
Phase 3: Rebase Workflows
Use rebase to maintain a clean, linear history.
-
Standard Rebase (update branch)
git fetch origin
git rebase origin/main
-
Interactive Rebase (clean up commits)
git rebase -i HEAD~N
Action guide:
| Action | When to Use |
|---|
pick | Keep commit as-is |
reword | Fix commit message only |
squash | Merge into previous, combine messages |
fixup | Merge into previous, discard message |
drop | Remove commit entirely |
-
Safe Rebase Checklist
AI Agent Note: Interactive commands (rebase -i, add -p) require terminal interaction. AI agents without interactive input support should use non-interactive alternatives (e.g., git rebase --onto, git add <specific-files>) or prepare the rebase todo list programmatically.
-
If Rebase Goes Wrong
git rebase --abort
git reflog
git reset --hard HEAD@{N}
Completion criteria:
Phase 4: Cherry-Pick
Safely move individual commits between branches.
-
Before Cherry-Picking
- Identify the exact commit(s):
git log --oneline source-branch
- Verify the commit is self-contained (atomic)
- Check for dependencies on other commits
-
Execute Cherry-Pick
git cherry-pick <commit-hash>
git cherry-pick <hash1> <hash2> <hash3>
git cherry-pick <older-hash>..<newer-hash>
-
Handle Cherry-Pick Conflicts
git status
git add <resolved-files>
git cherry-pick --continue
git cherry-pick --abort
-
Verify After Cherry-Pick
- Tests pass on the target branch
- No unintended side effects
- Commit message still makes sense in new context
Completion criteria:
Phase 5: History Cleanup
Fix mistakes without breaking history for others.
-
Amend Last Commit
git add <forgotten-file>
git commit --amend
git commit --amend -m "corrected message"
Only amend unpushed commits. Amending pushed commits requires force-push.
-
Fixup Older Commits
git commit --fixup=<target-hash>
git rebase -i --autosquash HEAD~N
-
Recovery with Reflog
git reflog
git cherry-pick <reflog-hash>
git reset --hard HEAD@{N}
-
Safety Rules
- Never rewrite history on shared branches without team agreement
- Always verify
git reflog shows the state you want to restore
- Create a backup branch before destructive operations:
git branch backup-before-cleanup
Completion criteria:
Phase 6: Conflict Resolution
Resolve merge conflicts systematically, not by guessing.
-
Understand the Conflict
git status
git diff --merge
git log --merge --oneline
-
Resolve Each File
- Read BOTH sides of the conflict markers
- Understand WHY each side made their change
- Choose the resolution that preserves BOTH intents
- If unsure, check the original commits for context
-
Resolution Strategies
| Situation | Strategy |
|---|
| One side is clearly correct | Take that side |
| Both changes needed | Combine manually |
| Changes are incompatible | Consult the other author |
| Complex logic conflict | Write a new implementation that satisfies both |
-
After Resolving
git add <resolved-files>
git commit
-
Verify Resolution
- Run tests — conflicts often introduce subtle bugs
- Review the merge commit diff — ensure nothing was lost
- Check that both features still work as intended
Completion criteria:
Verification Checklist
Before completing any git operation:
Red Flags — STOP
| Thought | Reality |
|---|
| "I'll clean up the history later" | Later never comes. Clean it now. |
| "Just one big commit is fine" | Big commits are impossible to review, revert, or bisect. |
| "Nobody reads commit messages" | git blame and git bisect users do. Future-you does. |
| "Force push is fine, I'm the only one on this branch" | Verify first. CI, bots, and reviewers may have fetched. |
| "The merge conflict is obvious, just take mine" | Obvious conflicts hide subtle logic bugs. Read both sides. |
| "I'll squash on merge anyway" | Squash loses the story. Clean history beats squashed history. |
| "Rebase is scary, I'll just merge" | Merge commits obscure linear history. Learn rebase. |
| "This commit message is good enough" | Future readers have zero context. Be specific. |
ALL of these mean: STOP. Follow the relevant phase.
Related Skills
| Skill | Relationship |
|---|
refactoring | Refactoring produces commits — use git-master for commit hygiene |
test-driven-development | TDD cycle maps to atomic commits: one test + impl per commit |
pr-all-in-one | PR preparation requires clean history — git-master feeds into it |
deployment-checklist | Deploy from clean branches with verified history |
Related Agents
| Agent | When to Involve |
|---|
| Code Reviewer | When commit organization affects review quality |
| DevOps Engineer | When branch strategy impacts CI/CD pipeline |