Skip to content
Visual explainer 2h to learn 3 steps

Resolving conflicts

Resolve merge and rebase conflicts calmly.

By Invicly Tech, , in Git, GitHub & GitHub Actions. About 10 minutes to read.

Topic 4 of 18

In short

A conflict happens when two branches change the same lines and Git cannot choose. Git marks the conflicting region in the file; you edit it to the correct result, then tell Git it is resolved.

Explained simply

A merge conflict sounds scary, but it only means Git found two changes to the same lines and refuses to guess which one is right. It pauses, marks the spot in the file, and waits for a human. Nothing is lost: both versions are right there.

Conflicts only happen when both sides touched the same lines, or lines right next to each other. If you changed the top of a file and a teammate changed the bottom, Git combines them on its own. If one person deleted a file the other edited, that is a conflict too.

Your job is to decide what the final code should be. Sometimes you keep one side, often you combine both, and sometimes you write something new. Then you remove the marker lines, run the tests, stage the file and continue.

The best way to avoid painful conflicts is to keep branches short and merge or rebase from main often. Two small conflicts this week are much easier than one giant conflict next month.

Press Play on a diagram to walk through it one step at a time.

FlowResolving a merge conflict
  1. 1
    Merge stops

    git status lists files as both modified.

  2. 2
    Read the markers

    Compare the ours and theirs sections in each file.

  3. 3
    Edit the result

    Keep, combine or rewrite; delete all marker lines.

  4. 4
    Test it

    Run the app or tests before finishing.

  5. 5
    Stage and continue

    git add, then git commit or git rebase --continue.

SequenceA rebase that stops on a conflict
You
Git
Working files
  1. 1git rebase origin/main
    You to Git: git rebase origin/main
  2. 2Replay commit 1: clean
    Git to Working files: Replay commit 1: clean
  3. 3Replay commit 2: write markers
    Git to Working files: Replay commit 2: write markers
  4. 4Stop: CONFLICT in config.py
    Git to You: Stop: CONFLICT in config.py
  5. 5Edit file, remove markers
    You to Working files: Edit file, remove markers
  6. 6git add, git rebase --continue
    You to Git: git add, git rebase --continue
  7. 7Replay commit 3: clean
    Git to Working files: Replay commit 3: clean
  8. 8Successfully rebased
    Git to You: Successfully rebased

In depth

Seeing the base with zdiff3

Default conflict markers show only two sides: yours between <<<<<<< and =======, theirs between ======= and >>>>>>>. That hides an important clue: what the code looked like before either change. Turn on git config --global merge.conflictStyle zdiff3 (Git 2.35 and later). Now each conflict also has a ||||||| section with the original lines from the merge base.

With the base visible, you can see what each side intended. If the base said TIMEOUT = 30, ours says 30 with a comment added and theirs says 60, the right answer is probably 60 with the comment. Without the base, you might think the comment was the change from theirs.

  • <<<<<<< starts our side (HEAD)
  • ||||||| shows the merge base (with diff3 or zdiff3)
  • ======= separates base or ours from theirs
  • >>>>>>> ends their side

The index holds three versions

During a conflict, the index stores up to three entries for the file, called stages. Stage 1 is the merge base, stage 2 is ours and stage 3 is theirs. git ls-files -u lists them. You can print any one: git show :1:app.py, :2:app.py or :3:app.py. This is handy for comparing side by side in your own tools.

To take one side for the whole file, use git checkout --ours app.py or git checkout --theirs app.py (git restore has the same flags). git add then collapses the stages into one normal entry, which is how Git knows the file is resolved. git diff during a conflict shows a combined diff against both sides.

MapA conflicted file in the index
app.py (unmerged)
  • Stage 1: base
    • Version before either change
    • git show :1:app.py
  • Stage 2: ours
    • Current branch (HEAD)
    • git checkout --ours app.py
  • Stage 3: theirs
    • Branch being merged in
    • git checkout --theirs app.py
  • After git add
    • One normal entry: resolved

Conflicts during a rebase feel backwards

A rebase replays your commits one at a time on top of main, so conflicts can appear once per commit instead of once in total. And the labels swap: during a rebase, ours (HEAD) is the new base, meaning main plus your commits replayed so far, and theirs is the commit of yours being replayed now. So --theirs keeps your change and --ours keeps main's.

After fixing each stop: git add the files, then git rebase --continue. If a commit becomes empty because main already has the same change, git rebase --skip drops it. If things get messy, git rebase --abort returns to the start. For long branches with many repeated conflicts, a single merge can be less work than a rebase.

Tools that make conflicts easier

git mergetool opens each conflicted file in a visual tool like VS Code, Meld or vimdiff, configured with merge.tool. Many editors also show buttons like Accept Current or Accept Incoming right above the markers.

If you resolve the same conflict over and over, for example while rebasing a long branch, turn on rerere (reuse recorded resolution) with git config --global rerere.enabled true. Git remembers how you resolved a conflict and applies the same fix automatically the next time it sees the identical conflict. Before committing, git diff --check warns if any conflict markers or whitespace errors are left in your unstaged changes (add --cached for staged ones).

  • git log --merge -p shows the commits that touched the conflicted files
  • git diff --name-only --diff-filter=U lists unresolved files
  • git merge -X theirs prefers their side only for conflicting hunks

Learn it in 3 short steps

About 40m each. Track them with a timer in your plan.

  1. 1
    Read conflict markers and diffs~40m
  2. 2
    Resolve a merge conflict~40m
  3. 3
    Resolve conflicts during a rebase~40m

Key points

  • Conflict markers are <<<<<<<, ======= and >>>>>>> around the two versions.
  • During a rebase, ours is the branch you rebase onto, theirs is your commit.
  • You can always back out with git merge --abort or git rebase --abort.
  • Small, frequent merges from main mean small, easy conflicts.

Key terms

Tap a card to flip it. 0/4 known

Examples

text
<<<<<<< HEAD
TIMEOUT = 30
=======
TIMEOUT = 60
>>>>>>> feature/slow-api

# after editing, the file just says:
TIMEOUT = 60

$ git add config.py
$ git commit

A conflict in config.py and its resolution.

Resolving a conflict with the base shown

You and a teammate both edited retry settings. With zdiff3 on, the conflict shows three versions. Step 1: read the base: retries was 3. Step 2: ours raised it to 5. Step 3: theirs kept 3 but added a backoff. Both intentions are valid, so the correct result keeps both: 5 retries with a backoff. Step 4: delete all marker lines, run the tests, git add and commit.

text
<<<<<<< HEAD
RETRIES = 5
||||||| 1a2b3c4
RETRIES = 3
=======
RETRIES = 3
BACKOFF_SECONDS = 2
>>>>>>> feature/backoff

# resolved:
RETRIES = 5
BACKOFF_SECONDS = 2

A lockfile conflict: regenerate, do not hand-edit

Both branches added an npm package, so package-lock.json conflicts in hundreds of lines. Editing it by hand is error-prone. Step 1: resolve package.json by hand (keep both dependencies). Step 2: take either lockfile version. Step 3: run npm install so npm rebuilds the lockfile from package.json. Step 4: stage both files and continue. The same idea works for poetry.lock, yarn.lock and other generated files.

bash
git status                         # both modified: package.json, package-lock.json
# edit package.json: keep both new dependencies
git checkout --theirs package-lock.json
npm install                        # regenerates the lockfile
npm test
git add package.json package-lock.json
git commit                         # or: git rebase --continue

Quick reference

  • git config --global merge.conflictStyle zdiff3 shows the base version in conflicts
  • git diff --name-only --diff-filter=U lists files that still have conflicts
  • git checkout --ours file or --theirs file takes one side for the whole file
  • During a rebase, --theirs is YOUR commit and --ours is the branch you rebase onto
  • git merge --abort and git rebase --abort return to the state before you started
  • git rebase --skip drops a commit that became empty or is not needed
  • git config --global rerere.enabled true reuses resolutions you already made
  • git diff --check catches leftover conflict markers before you commit

Common mistakes

  • Leaving a stray ======= or <<<<<<< line in the file and committing it.
  • Resolving by blindly picking one side and silently dropping a teammate's change.

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1What does the ||||||| section in a conflict show?

Question 2

2You are rebasing feature onto main and want to keep your own version of a conflicted file. Which command?

Question 3

3How does Git know you have resolved a conflicted file?

Question 4

4Which setting makes Git repeat a conflict resolution you already did?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Create a conflict on purpose: change the same line of a file differently on main and on a branch, merge, then abort.

  2. Exercise 2medium

    Repeat the conflict, turn on zdiff3, and resolve it by combining both sides into one line, then finish the merge.

  3. Exercise 3hard

    Rebase a branch with three commits that each conflict with main. Resolve the first, then use rerere so a repeated identical conflict resolves itself after you abort and retry.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Why do I get the same conflict again and again during a rebase?
A rebase replays each commit separately, so if several of your commits touch the same lines, each one can conflict. Squash those commits first, enable rerere, or merge instead.
Can I resolve conflicts on GitHub instead of locally?
Yes, for simple text conflicts GitHub offers a web editor on the pull request. Complex conflicts, or ones that need testing, are better resolved locally.
What does "deleted by them" mean in git status?
One side deleted the file and the other changed it. Decide whether it should exist: git rm the file to accept the deletion, or git add it to keep your version.

Go deeper

ShareXLinkedInWhatsApp