Skip to the content

Lesson 96 of 296

Merge conflicts; rebase vs merge

What you will learn

  • Why conflicts happen
  • Reading conflict markers
  • Resolving a conflict step by step
  • What rebase does
  • Interactive rebase to tidy commits
  • The golden rule of rebasing
  • Force pushing safely
  • Merge vs rebase: when to use each
On this page

Why conflicts happen

A merge conflict happens when two branches change the same lines of the same file differently, or one branch edits a file that the other deleted. Git cannot know which version is right, so it asks you.

Conflicts are normal. They are not a sign that you did something wrong.

What a conflict looks like

git merge feature/discount
CONFLICT (content): Merge conflict in src/price.js
Automatic merge failed; fix conflicts and then commit the result.

Inside src/price.js:

function finalPrice(pricePence) {
<<<<<<< HEAD
  return Math.round(pricePence * 1.2);
=======
  return applyDiscount(pricePence) * 1.2;
>>>>>>> feature/discount
}
MarkerMeaning
<<<<<<< HEADStart of your current branch's version
=======The divider
>>>>>>> feature/discountEnd of the other branch's version

Resolving it, step by step

  1. Run git status to see every file with conflicts ("both modified").
  2. Open each file and find the markers.
  3. Decide the correct result. It may be one side, the other side, or a combination of both. Here both changes matter:
function finalPrice(pricePence) {
  return Math.round(applyDiscount(pricePence) * 1.2);
}
  1. Delete all the markers.
  2. Run the code and the tests. A conflict-free file can still be logically broken.
  3. Stage and finish:
git add src/price.js
git commit

To give up and go back to how things were before the merge:

git merge --abort

VS Code and other editors show conflicts with buttons such as "Accept Current", "Accept Incoming" and "Accept Both". They help, but you must still understand the code. Talk to the teammate who wrote the other change if you are unsure.

Reducing conflicts

  • Keep branches short-lived.
  • Pull main into your branch (or rebase) often.
  • Keep PRs small.
  • Agree on a formatter (such as Prettier), so formatting changes do not cause conflicts.

What rebase does

Rebase moves your branch's commits so they start from the latest commit of another branch. Git replays your commits, one by one, on top.

Before:
          A - B - D - E   <- main
               \
                C - F     <- feature

git switch feature
git rebase main

After:
          A - B - D - E           <- main
                       \
                        C' - F'   <- feature

C' and F' contain the same changes as C and F, but they are new commits with new hashes, because their parent changed.

The result is a linear history, as if you had started your work from the latest main.

If a replayed commit conflicts, Git stops. Fix the files, then:

git add <file>
git rebase --continue

Or cancel the whole rebase with git rebase --abort.

Interactive rebase: tidy your commits

Before opening a PR, you can clean up messy commits ("wip", "fix typo", "oops"):

git rebase -i HEAD~4

Git opens a list of the last 4 commits. Change the word at the start of each line:

WordEffect
pickKeep the commit as it is
rewordKeep it, but edit the message
squashCombine with the commit above, and edit the combined message
fixupCombine with the commit above, and discard this message
dropDelete the commit

You can also reorder the lines to reorder the commits.

The golden rule of rebasing

Never rebase commits that other people have already pulled.

Rebasing creates new commits with new hashes. If teammates built work on the old commits, their history no longer matches yours, which causes duplicated commits and painful conflicts.

Safe: rebasing your own feature branch that nobody else works on.
Not safe: rebasing main or a shared branch.

Force pushing

After rebasing a branch you already pushed, a normal push is rejected, because the history changed. You must force push:

git push --force-with-lease

Always use --force-with-lease instead of --force. It refuses to overwrite the remote branch if someone else pushed to it since your last fetch. Plain --force silently deletes their work.

Merge vs rebase

MergeRebase
HistoryKeeps the real history, including when branches split and joinedRewrites your commits into a straight line
Creates new commitsOne merge commitNew copies of every rebased commit
Safe on shared branchesYesNo
ConflictsResolved once, in the merge commitMay need resolving once per replayed commit
History readabilityCan become cluttered with merge commitsClean and linear

Common team practice:

  • Use rebase (or git pull --rebase) to update your own feature branch with the latest main, and interactive rebase to tidy commits before review.
  • Use merge (often squash and merge on GitHub) to bring a finished PR into main.
  • Never rebase shared branches.

Many developers set git config --global pull.rebase true so git pull rebases local commits instead of creating small merge commits.

Common mistakes

  • Committing with conflict markers still in the file.
  • Choosing one side without understanding the other change.
  • Rebasing a shared branch.
  • Using --force instead of --force-with-lease.
  • Panicking during a rebase. git rebase --abort always takes you back to the start.

How to say it in an interview

"A conflict happens when both branches change the same lines. I check git status, open each file, decide the correct combined result, remove the markers, run the tests, then stage and commit; git merge --abort cancels if needed. Rebase replays my commits on top of another branch, creating new commits and a linear history. I rebase my own feature branch to keep it up to date and use interactive rebase to squash fixup commits, but never rebase shared history, and I force push with force-with-lease. Merge preserves the true history and is safe on shared branches."

Key words

WordMeaning
Merge conflictGit cannot combine changes automatically
Conflict markers<<<<<<<, =======, >>>>>>>
RebaseReplaying commits on top of another branch
Interactive rebaseEditing, combining or reordering commits
SquashCombining several commits into one
Linear historyA history with no branching lines
Force pushOverwriting remote history
--force-with-leaseA safer force push that protects others' work

Now prove it

This lesson is complete when you pass the quiz and all three practice levels.