bashadvanced

Git Rebase Strategies Guide

Advanced rebase strategies for cleaning history including onto, autosquash, and conflict resolution.

bash
# Basic rebase onto main
git checkout feature-branch
git rebase main

# Interactive rebase to clean last 5 commits
git rebase -i HEAD~5
# In editor:
# pick abc1234 Add feature A
# squash def5678 Fix typo in A   ← combines with previous
# pick ghi9012 Add feature B
# reword jkl3456 Old message     ← edit commit message
# drop mno7890 Debug logging     ← remove commit entirely

# Rebase --onto: move branch to different base
# Move feature from old-base to new-base
git rebase --onto new-base old-base feature-branch

# Autosquash: auto-arrange fixup commits
git commit --fixup abc1234
# Later:
git rebase -i --autosquash main

# Rebase preserving merge commits
git rebase --rebase-merges main

# Abort a rebase in progress
git rebase --abort

# Continue after resolving conflicts
git add resolved-file.ts
git rebase --continue

# Skip a conflicting commit
git rebase --skip

# Use rerere to remember conflict resolutions
git config rerere.enabled true
# Git will auto-apply same resolution next time

# Pull with rebase instead of merge
git pull --rebase origin main

Use Cases

  • Cleaning commit history before merging
  • Moving branches to a new base
  • Automated fixup commit squashing

Tags

Related Snippets

Similar patterns you can reuse in the same workflow.