Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 42Git · CI/CD

How to resolve a Git merge conflict

Two people changed the same lines, and Git can not decide which version is right — so it stops and asks you. A conflict is normal, not an error. This how-to shows how to read the conflict markers, choose or combine the changes, finish (or stop) the merge or rebase, the "ours vs theirs" trap during a rebase, and habits that make conflicts rare.

  • 5 Lessen
  • 4 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

42.1

The quick answer

git merge feature/login-limit
CONFLICT (content): Merge conflict in app/config.py
Automatic merge failed; fix conflicts and then commit the result.
git status
git add app/config.py
git commit
git merge --abort

Fix the file, git add it, git commit (or git merge --continue). git merge --abort goes back to before the merge.

Commando's in deze les

42.2

Step by step

  1. List the files with conflicts:
git status
Unmerged paths:
  both modified:   app/config.py
git diff --name-only --diff-filter=U
  1. Read the markers in each file:
app/config.py
<<<<<<< HEAD
LOGIN_RATE_LIMIT = "10/minute"
=======
LOGIN_RATE_LIMIT = "5/minute"
LOGIN_LOCKOUT_MINUTES = 15
>>>>>>> feature/login-limit

The part between <<<<<<< HEAD and ======= is your current branch; between ======= and >>>>>>> is the branch you merge in.

  1. Decide and edit — keep one side, or combine both. Remove all three marker lines:
app/config.py
LOGIN_RATE_LIMIT = "5/minute"
LOGIN_LOCKOUT_MINUTES = 15
  1. Take one whole side for a file (for example a generated lock file):
git checkout --theirs package-lock.json
git checkout --ours docs/CHANGELOG.md
git add package-lock.json docs/CHANGELOG.md

For lock files, often the best way is: take one side, then run the tool again (npm install) and commit the new lock file.

  1. Finish:
git add app/config.py
git commit
git push

Run the tests before you push — the merged code is new code.

  1. During a rebase the words switch places: --ours is the branch you rebase onto (main), --theirs is your commit. Continue commit by commit:
git rebase main
git add app/config.py
git rebase --continue
git rebase --abort
  1. Make conflicts rare: small pull requests, merge main into your branch (or rebase) often, agree on formatters (Black, Prettier) so style changes do not collide, and let Git remember your resolutions:
git config --global rerere.enabled true
git config --global merge.conflictstyle zdiff3

zdiff3 also shows the original lines between the two sides — much easier to decide.

42.3

Check that it worked

git status
grep -rnE '^(<<<<<<<|=======|>>>>>>>)' --include='*.py' . || echo "no markers left"
git log --oneline --graph -5

No unmerged paths, no markers left in the code, tests pass, and the log shows the merge commit.

42.4

Common problems

ProblemCauseFix
Markers were committed by mistakeThe file was added before it was fixed.Fix it in a new commit; add a CI check (or pre-commit) that fails on markers.
You have not concluded your merge (MERGE_HEAD exists)A merge is still open.Finish (git commit) or git merge --abort.
--ours took the wrong sideDuring a rebase ours/theirs are switched.Check again: in a rebase, --theirs = your commit.
The same conflict on every rebaseLong-living branch, conflict repeats per commit.rerere; or squash your commits first; or merge instead of rebase.
Binary file conflict (image, PDF)Git can not mix binary files.Choose one: git checkout --ours/--theirs FILE.
Code merged without conflict but is brokenBoth changes are fine alone but not together.Tests in CI on the merged result; required status checks.

Commando's in deze les

42.5

Go further

1In a merge conflict, which part is between <<<<<<< HEAD and =======?

The version of your current branch.

2During git rebase main, what does git checkout --theirs FILE take?

Your own commit's version — in a rebase, ours and theirs are switched.

Commando's in deze les

Alle commando's in dit hoofdstuk