What's Actually Happening
Rebase replays your commits one-by-one onto a new base. Each commit can conflict with the new base. Git pauses at each conflict, letting you resolve before continuing. If conflicts are too many or complex, you can abort and return to original state.
The Error You'll See
$ git rebase origin/main
Rebasing (1/5)
CONFLICT (content): Merge conflict in src/app.js
error: could not apply abc123d... My commit message
Resolve all conflicts manually, mark them as resolved with
"git add/rm <conflicted_files>", then run "git rebase --continue".
You can instead skip this commit with "git rebase --skip".
To abort and get back to the state before "git rebase", run "git rebase --abort".Why This Happens
- 1.Commits touch same code as base - Your changes overlap with upstream
- 2.Rebase order causes conflicts - Earlier commits break later ones
- 3.Base branch diverged heavily - Many changes between base points
- 4.Renamed or deleted files - Base branch changed file structure
Step 1: Check Rebase Status
git statusShows:
``` interactive rebase in progress; onto def4567 Last command done (1 command done): pick abc123d My commit message Next commands to do (3 remaining commands): pick ghi890j Second commit pick jkl234m Third commit (use "git rebase --edit-todo" to view and edit)
Unmerged paths: (use "git restore --staged <file>..." to unstage) (use "git add/rm <file>..." as appropriate to mark resolution) both modified: src/app.js ```
Step 2: See What's Conflicting
```bash # List conflicted files git diff --name-only --diff-filter=U
# See conflict details git diff src/app.js ```
Step 3: Resolve the Conflict
Open conflicted file and find markers:
<<<<<<< HEAD
new base version
=======
my commit version
>>>>>>> abc123d (My commit message)Edit to resolve:
```bash # Fix manually vim src/app.js
# Or pick one side git checkout --ours src/app.js # Keep base version git checkout --theirs src/app.js # Keep your commit version
# Mark resolved git add src/app.js ```
Step 4: Continue Rebase After Resolution
git rebase --continueGit applies next commit. If more conflicts, repeat steps 3-4.
Step 5: Skip Problematic Commit
If a commit causes issues and you want to drop it:
git rebase --skipThis skips the current commit and continues with next one.
Step 6: Abort Rebase Entirely
If rebase is too problematic:
git rebase --abortReturns to exact state before rebase started:
git log --oneline -3
# Shows original commits intactStep 7: Edit Rebase Plan Mid-Rebase
If you want to change remaining commits:
git rebase --edit-todoOpens editor with remaining commits. You can change pick to:
- drop - Remove commit
- edit - Pause at commit to amend
- squash - Combine with previous
- reword - Change commit message
Step 8: Use Git Rerere for Repeated Conflicts
Enable rerere to remember resolutions:
```bash # Enable rerere git config --global rerere.enabled true
# Git remembers how you resolve conflicts # Same conflict later gets auto-resolved ```
Step 9: Check Reflog for Safety
If rebase went very wrong and abort didn't help:
git reflogFind pre-rebase state:
abc123d HEAD@{0}: rebase: checkout def4567
original HEAD@{1}: checkout: moving to main
ghi890j HEAD@{2}: commit: Last commit before rebaseReset to pre-rebase:
git reset --hard HEAD@{2}Step 10: Rebase with Better Strategy
For complex rebases, use better approach:
```bash # Rebase with autosquash for fixup commits git rebase -i --autosquash origin/main
# Rebase with rerere enabled git rebase --rerere-autoupdate origin/main
# Create backup branch first git branch backup-before-rebase git rebase origin/main ```
Verify the Fix
After successful rebase:
```bash # Check rebase completed git status # Should show: nothing to commit, clean
# Check commit history git log --oneline -5
# Verify branch is based correctly git log --oneline --graph -10 ```
Prevention Tips
Before rebase:
```bash # Fetch latest git fetch origin
# Check what will be rebased git log origin/main..HEAD --oneline
# Create backup git branch backup-$(git branch --show-current)
# Rebase interactively for control git rebase -i origin/main ```