Resolving conflicts
Resolve merge and rebase conflicts calmly.
By Invicly Tech, , in Git, GitHub & GitHub Actions. About 10 minutes to read.
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.
- 1Merge stops
git status lists files as both modified.
- 2Read the markers
Compare the ours and theirs sections in each file.
- 3Edit the result
Keep, combine or rewrite; delete all marker lines.
- 4Test it
Run the app or tests before finishing.
- 5Stage and continue
git add, then git commit or git rebase --continue.
- You to Git: git rebase origin/main1git rebase origin/main
- Git to Working files: Replay commit 1: clean2Replay commit 1: clean
- Git to Working files: Replay commit 2: write markers3Replay commit 2: write markers
- Git to You: Stop: CONFLICT in config.py4Stop: CONFLICT in config.py
- You to Working files: Edit file, remove markers5Edit file, remove markers
- You to Git: git add, git rebase --continue6git add, git rebase --continue
- Git to Working files: Replay commit 3: clean7Replay commit 3: clean
- Git to You: Successfully rebased8Successfully 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.
- 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.
- 1Read conflict markers and diffs~40m
- 2Resolve a merge conflict~40m
- 3Resolve 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
<<<<<<< HEAD
TIMEOUT = 30
=======
TIMEOUT = 60
>>>>>>> feature/slow-api
# after editing, the file just says:
TIMEOUT = 60
$ git add config.py
$ git commitA 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.
<<<<<<< HEAD
RETRIES = 5
||||||| 1a2b3c4
RETRIES = 3
=======
RETRIES = 3
BACKOFF_SECONDS = 2
>>>>>>> feature/backoff
# resolved:
RETRIES = 5
BACKOFF_SECONDS = 2A 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.
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 --continueQuick 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.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Create a conflict on purpose: change the same line of a file differently on main and on a branch, merge, then abort.
- Exercise 2medium
Repeat the conflict, turn on zdiff3, and resolve it by combining both sides into one line, then finish the merge.
- 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.