| name | git-rebase |
| description | Intelligently handle git rebase operations and resolve merge conflicts while preserving features and maintaining code quality. Use when rebasing feature branches, resolving conflicts across commits, and ensuring clean linear history without losing changes. |
Git Rebase with Intelligent Conflict Resolution
Quick Start
For most rebases with multiple commits, use the squash-first strategy to resolve conflicts only once:
bash scripts/pre-rebase-backup.sh
git rebase -i $(git merge-base HEAD origin/main)
git rebase origin/main
git push origin $(git rev-parse --abbrev-ref HEAD) --force-with-lease
This approach resolves conflicts once instead of per-commit, saving time and mental overhead.
Core Workflow: Conflict Analysis & Resolution
Copy this checklist and mark progress:
Rebase Workflow:
- [ ] Step 1: Create safety backup
- [ ] Step 2: Fetch latest from target branch
- [ ] Step 3: Analyze conflict scope
- [ ] Step 4: Choose resolution strategy
- [ ] Step 5: Apply conflict resolutions
- [ ] Step 6: Validate merged code
- [ ] Step 7: Run tests
- [ ] Step 8: Force push safely
Step 1: Create Safety Backup
ALWAYS do this first. If rebase goes wrong, you can recover:
bash scripts/pre-rebase-backup.sh
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
git branch backup-rebase-$TIMESTAMP
git reflog
This costs nothing and saves hours of work if something goes wrong.
Step 2: Fetch Latest Changes
Ensure you have the most recent remote state:
git fetch origin
git log --oneline origin/main..HEAD
git log --oneline HEAD..origin/main
Understand how many commits you're rebasing and how much main has changed.
Step 3: Analyze Conflict Scope
Before starting the rebase, predict conflicts:
git diff --name-only origin/main...HEAD
git diff --name-only origin/main HEAD
Key insight: If you changed auth.ts and so did main, you WILL get conflicts in auth.ts.
Anticipating conflicts helps you understand how to resolve them.
Step 4: Choose Resolution Strategy
For detailed strategy comparison and decision matrix, see references/strategies.md.
Strategy A: Squash First (Recommended for 3+ commits)
When to use: Multiple feature commits with many conflicts expected
Why: Reduces conflicts to one resolution phase instead of per-commit
git rebase -i $(git merge-base HEAD origin/main)
git rebase origin/main
Tradeoffs: Lose individual commit history, but simpler conflict resolution
Strategy B: Interactive Rebase with Conflict Awareness
When to use: 1-2 commits, clean history, or complex per-commit logic
git rebase -i origin/main
Tradeoffs: More control, but more conflict-resolution iterations
Strategy C: Simple Linear Rebase (Fastest, Auto-Resolution)
When to use: Simple cases, no critical decisions, or in automated pipelines
git rebase origin/main
Warning: Not recommended for complex scenarios. Use Strategies A or B instead.
Step 5: Apply Conflict Resolutions
When git rebase pauses with conflicts, use the analysis script:
bash scripts/analyze-conflicts.sh
git status
git mergetool --no-prompt
Conflict Marker Anatomy
<<<<<<< HEAD
function authenticate(token) {
validateToken(token);
return true;
}
=======
function authenticate(token) {
if (!token) throw new Error("No token");
validateToken(token);
setSession(token);
return true;
}
>>>>>>> origin/main
Decision framework (before deleting markers):
-
Can you keep both? YES → Merge them intelligently
function authenticate(token) {
if (!token) throw new Error("No token");
validateToken(token);
setSession(token);
return true;
}
-
Conflicting logic? Understand WHY they differ, then decide
- Did main add critical security checks? → Keep main's version
- Did your feature add essential functionality? → Keep feature's version
- Are they trying to do different things? → Combine intentionally
-
Lost features? NEVER let a feature silently disappear
- If you added authentication logic, ensure it's in final version
- If main improved database access, ensure that's preserved
For detailed resolution patterns, see references/resolution-patterns.md.
Key Resolution Principles
✅ DO:
- Keep both versions' important functionality when possible
- Use the merge tool for visual representation
- Add comments explaining merged conflicts:
// Merged from both versions: main's validation + feature's session setup
- Test each file after resolution
❌ DON'T:
- Mindlessly pick one version without understanding both
- Delete conflict markers without understanding the conflict
- Keep duplicate code - merge intelligently
- Skip testing before continuing
Step 6: Validate Merged Code
After resolving conflicts, validate the merged code:
bash scripts/validate-merge.sh
npm run lint
npm run type-check
git diff HEAD origin/main -- <conflicted-file>
Important: Validation catches mistakes BEFORE you commit them.
Step 7: Run Tests
This is your safety net:
npm test
npm test -- --testPathPattern=auth
Rule: Never force-push code that fails tests.
Step 8: Force Push Safely
Use --force-with-lease instead of --force. It protects against accidentally overwriting others' work:
git push origin $(git rev-parse --abbrev-ref HEAD) --force-with-lease
git push origin your-branch -f
Common Scenarios & Strategies
Scenario 1: Many Small Conflicts Across 5+ Commits
Use: Squash-first strategy
git rebase -i $(git merge-base HEAD origin/main)
git rebase origin/main
git rebase --continue
Why: Each commit might have conflicts. Squashing before rebasing means one pass.
Scenario 2: One Specific Commit Has Conflicts
Use: Target that commit with interactive rebase
git rebase -i origin/main
git status
git rebase --continue
Why: Isolating the commit helps you understand what it's trying to do.
Scenario 3: Conflicts Keep Repeating (Same File, Different Commits)
Use: Git rerere (reuse recorded resolution)
git config --global rerere.enabled true
git rebase origin/main
Why: When rebasing and hitting the same file repeatedly, rerere saves manual work.
Scenario 4: Rebase Conflicts Are Too Complex
Emergency escape plan:
git rebase --abort
git merge origin/main
Important: It's okay to abort and rethink. Better than a broken rebase.
Bundled Scripts
This skill includes helper scripts in scripts/:
pre-rebase-backup.sh: Creates a safety backup before rebasing
analyze-conflicts.sh: Analyzes current conflicts and provides detailed information
validate-merge.sh: Validates that merge/rebase is clean and ready for testing
Run scripts with bash scripts/<script-name>.sh.
Reference Files
For detailed information on specific topics:
Checklist: Before You Commit to Rebasing
When NOT to Rebase
- Shared branches (use merge instead:
git merge origin/main)
- Critical production code without comprehensive tests
- When multiple people are pushing to the same branch
- If you don't understand the conflicts you're seeing
Default to merge if uncertain. Merge is safer for collaborative work.
Use rebase when: Solo feature branch, clean history matters, no shared dependencies.
Summary: The Rebase Philosophy
Rebasing is a tool for creating clean, linear commit history. Used well, it makes debugging and code review easier. Used poorly, it loses work.
The key principle: Understand every conflict before resolving it. Don't automate away the thinking.
With this Skill, you can rebase with confidence, understanding each decision and protecting your code throughout the process.