Rebase conflicts occur when Git tries to apply your commits onto a new base but finds overlapping changes. Unlike merge conflicts, you resolve conflicts for each commit individually.

The Error

During rebase:

bash
Executing: git rebase origin/main
CONFLICT (content): Merge conflict in src/app.js
error: could not apply abc123... Fix authentication bug
hint: Resolve all conflicts manually, mark them as resolved
hint: with "git add/rm <file>", then run "git rebase --continue".
hint: If you prefer to skip this patch, run "git rebase --skip" instead.
hint: To check out the original branch and abort the rebase,
hint: run "git rebase --abort".

Status shows:

``` interactive rebase in progress; onto def456 Last command done (1 command done): pick abc123 Fix authentication bug No commands remaining. You are currently rebasing branch 'feature' on 'def456'. (fix conflicts and then run "git rebase --continue") (use "git rebase --skip" to skip this patch) (use "git rebase --abort" to check out the original branch and abort the rebase)

Unmerged paths: (use "git add <file>..." to mark resolution) both modified: src/app.js ```

Why This Happens

Rebase replays your commits one by one onto a new base. Each commit application is like a mini-merge. If the base changed in the same lines your commit modifies, Git pauses for resolution.

Common causes: - Your branch and main both modified the same code - Someone else fixed the same bug differently - Refactoring on main conflicts with your feature changes - Your commit is based on old code that no longer exists

Diagnosis Steps

Check which commit caused conflict: ``bash git status

Shows the commit being applied.

See remaining commits: ``bash cat .git/rebase-merge/git-rebase-todo

Shows which commits are left to replay.

View the conflict: ``bash git diff --name-only --diff-filter=U

See what the incoming commit changes: ``bash git show HEAD

Compare with base: ``bash git diff def456..HEAD

Solution 1: Resolve Conflict and Continue

Edit conflicted files: ``bash nano src/app.js

Remove conflict markers and choose correct content.

Stage resolved files: ``bash git add src/app.js

Continue rebase: ``bash git rebase --continue

Git applies the resolved commit and moves to the next one. May encounter more conflicts.

Solution 2: Abort the Rebase

If conflicts are too complex or you want different approach:

bash
git rebase --abort

Returns to original state before rebase started. All your commits are preserved on original branch.

Solution 3: Skip the Conflicting Commit

If the commit is no longer needed (bug already fixed on main):

bash
git rebase --skip

Discards the current commit and moves to next. Use only when the change is truly redundant.

Solution 4: Take One Side Completely

For specific files, choose one version:

Keep your commit's version: ``bash git checkout --ours src/app.js git add src/app.js git rebase --continue

Keep the base's version: ``bash git checkout --theirs src/app.js git add src/app.js git rebase --continue

Note: During rebase, --ours is the base you're rebasing onto, --theirs is your commit being applied.

Solution 5: Edit Commit During Rebase

Change the commit content while resolving:

bash
# After resolving conflicts and staging
git commit --amend
# Edit message or content
git rebase --continue

Solution 6: Use Merge Tool

Launch visual merge tool:

bash
git mergetool

Configure your preferred tool:

bash
git config --global merge.tool vscode
git config --global mergetool.vscode.cmd 'code --wait $MERGED'

Solution 7: Restart Rebase Differently

Abort and restart with different strategy:

bash
git rebase --abort
git rebase -i origin/main

In interactive mode, you can: - Reorder commits - Squash commits together - Drop problematic commits - Edit commits before applying

Solution 8: Multiple Conflicts Sequentially

Rebase may hit conflicts on multiple commits:

```bash # Resolve first conflict git add src/app.js git rebase --continue

# Hit second conflict git add src/config.js git rebase --continue

# Hit third conflict git add src/utils.js git rebase --continue ```

Work through each one. If one is too difficult, abort and reconsider.

Solution 9: Rebase Onto Different Base

If current base is problematic:

bash
git rebase --abort
git rebase --onto def456 abc123 feature

Rebases onto def456, excluding commits before abc123.

Solution 10: Find Lost Commits After Abort

If you abort but can't find original state:

bash
git reflog

Look for state before rebase:

bash
abc123 HEAD@{0}: rebase (abort): updating HEAD
def456 HEAD@{1}: rebase (start): checkout def456
ghi789 HEAD@{2}: checkout: moving from main to feature

Reset to original:

bash
git reset --hard HEAD@{2}

Verification

Confirm rebase completed: ``bash git status

Should show: `` On branch feature nothing to commit, working tree clean

Check commit history: ``bash git log --oneline --graph -10

Your commits should appear on top of base.

Verify no conflict markers: ``bash git diff --check

No output means clean resolution.

Confirm file content: ``bash cat src/app.js

Should have your expected changes.

Test functionality: ``bash npm test make check

Ensure rebase didn't break anything.

Prevention Strategies

Update your branch frequently: ``bash git fetch origin git rebase origin/main

Regular rebasing reduces divergence.

Keep commits small and focused: Smaller commits have smaller conflict surfaces.

Use feature flags: Isolate new code to reduce conflicts with main.

Review before rebase: ``bash git log origin/main..HEAD --oneline git diff origin/main..HEAD --stat

Anticipate potential conflicts.

Rebase vs Merge for Conflicts

Rebase conflicts resolve each commit individually - tedious but cleaner history.

Merge conflicts resolve all at once - simpler but creates merge commit.

For complex conflicts, consider merge:

bash
git rebase --abort
git merge origin/main

Resolve all conflicts in one step.