Loading...
Loading...
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.
npx skill4agent add pedronauck/skills git-rebase# Step 1: Backup current state
bash scripts/pre-rebase-backup.sh
# Step 2: Squash commits (interactive rebase on current branch)
git rebase -i $(git merge-base HEAD origin/main)
# Step 3: Rebase onto target
git rebase origin/main
# Step 4: If conflicts, resolve them once (see workflow below)
# Then continue: git rebase --continue
# Step 5: Force push safely
git push origin $(git rev-parse --abbrev-ref HEAD) --force-with-leaseRebase 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# Use the bundled script
bash scripts/pre-rebase-backup.sh
# Or manually create timestamped backup branch
TIMESTAMP=$(date +%Y%m%d_%H%M%S)
git branch backup-rebase-$TIMESTAMP
# Alternative: Create temporary ref to your current commit
git reflog # Note your current HEAD SHA for manual recovery# Fetch without modifying local branches
git fetch origin
# View the divergence
git log --oneline origin/main..HEAD # Your commits
git log --oneline HEAD..origin/main # New commits on main# See which files you changed
git diff --name-only origin/main...HEAD
# See which files main changed
git diff --name-only origin/main HEAD
# Likely conflict areas: files changed in bothauth.tsauth.ts# Interactive rebase on current branch first
git rebase -i $(git merge-base HEAD origin/main)
# In editor, change all "pick" to "squash" (or 's') except first commit
# Save and exit - commits are squashed into one
# Edit commit message to describe the entire feature
# Now rebase the squashed commit
git rebase origin/main
# Resolve conflicts once, then git rebase --continuegit rebase -i origin/main
# In editor, you can:
# - Reorder commits to isolate conflict-prone ones
# - Drop commits that are already in main (git detects this)
# - Combine related commits before rebasing
# Save and exit - rebase proceeds, stopping at conflicts# Rebase all commits at once
git rebase origin/main
# If no conflicts, done
# If conflicts, you resolve each onegit rebase# Analyze conflicts
bash scripts/analyze-conflicts.sh
# See which files conflict
git status
# For each conflicted file:
# - RECOMMENDED: Use merge tool for visual clarity
git mergetool --no-prompt
# - ALTERNATIVE: Manual edit in your editor
# Search for conflict markers: <<<<<<, ======, >>>>>><<<<<<< HEAD
// Your current feature code
function authenticate(token) {
validateToken(token);
return true;
}
=======
// Main branch code (incoming)
function authenticate(token) {
if (!token) throw new Error("No token");
validateToken(token);
setSession(token);
return true;
}
>>>>>>> origin/mainfunction authenticate(token) {
if (!token) throw new Error("No token"); // Keep main's validation
validateToken(token);
setSession(token); // Keep main's session setup
return true; // Keep feature's return
}// Merged from both versions: main's validation + feature's session setup# Use the validation script
bash scripts/validate-merge.sh
# Manual checks:
# 1. Check syntax
npm run lint # or eslint, pylint, etc.
# 2. Check types (if TypeScript)
npm run type-check # or tsc --noEmit
# 3. Spot-check key files
git diff HEAD origin/main -- <conflicted-file>
# If validation fails:
# 1. Fix the issue in the file
# 2. git add <file>
# 3. git rebase --continue# Run full test suite
npm test
# Or specific tests for changed areas
npm test -- --testPathPattern=auth # If auth.ts was changed
# If tests fail:
# 1. Understand what broke
# 2. Fix in the files
# 3. git add <files>
# 4. git rebase --continue--force-with-lease--force# SAFE: Protects others' commits
git push origin $(git rev-parse --abbrev-ref HEAD) --force-with-lease
# UNSAFE: Can overwrite others' work
git push origin your-branch -f # Don't do this
# If force-with-lease fails:
# Someone else pushed to your branch
# Coordinate with them before forcinggit rebase -i $(git merge-base HEAD origin/main)
# Mark all but first commit as 's' (squash)
# Save - commits squash into one
git rebase origin/main
# Resolve conflicts once
git rebase --continue
# Only one conflict-resolution phase!git rebase -i origin/main
# In editor, move the problematic commit to the end
# Save - rebase proceeds, stopping at that commit
# When it stops, you know exactly which commit conflicts
git status # See what changed in this commit
# Resolve, then continue
git rebase --continue# Enable rerere globally (one-time setup)
git config --global rerere.enabled true
# Now when you hit the same conflict in a second commit,
# Git automatically applies the first commit's resolution
git rebase origin/main
# Git remembers your first conflict resolution
# and replays it automatically for similar conflicts# Abort the rebase - return to original state
git rebase --abort
# Fall back to merge (safer for complex scenarios)
git merge origin/main
# Or try a different approach:
# - Squash your entire feature branch first
# - Cherry-pick main's critical changes selectivelyscripts/pre-rebase-backup.shanalyze-conflicts.shvalidate-merge.shbash scripts/<script-name>.shbash scripts/pre-rebase-backup.shgit fetch origingit merge origin/main