The Plain Picture#
Two people edited copies of the same document. To combine them, you go through both. Where they changed different paragraphs, you simply take both changes. Where they changed the same sentence differently, a human has to decide what the sentence should say.
The Real Names#
Merging combines the changes from one branch into another:
git merge bonus(run while onmain) brings bonus's work into main.Fast-forward merge: if
mainhasn't moved sincebonusbranched off, Git just slidesmain's pointer forward to bonus's commit. No new commit is needed.Three-way merge: if both branches have new commits, Git compares three snapshots — the two branch tips and their merge base (the last commit they share) — and creates a merge commit with two parents.
A merge conflict happens when both branches changed the same lines differently. Git stops and asks you to decide.
Under the Hood — a real conflict#
On bonus: return correct * 10 + 5 ("Add bonus points").
On main: return correct * 20 ("Double points").
$ git merge bonus
Auto-merging quiz.py
CONFLICT (content): Merge conflict in quiz.py
Automatic merge failed; fix conflicts and then commit the result.Git writes both versions into the file, between conflict markers:
def score(correct):
<<<<<<< HEAD
return correct * 20
=======
return correct * 10 + 5
>>>>>>> bonusBetween
<<<<<<< HEADand=======: your current branch's version (main).Between
=======and>>>>>>> bonus: the incoming branch's version.
To resolve: edit the file into what it should be — here both ideas combined — and delete the markers:
def score(correct):
return correct * 20 + 5git add quiz.py # "I've resolved this file"
git commit --no-edit # finish the merge commit with the default messageThe resulting history, from git log --oneline --graph --all:
* 80b9b6d Merge branch 'bonus' ← merge commit: two parents
|\
| * 9ffbfe2 Add bonus points ← bonus's work
* | 640a7d3 Double points ← main's work
|/
* 7ef9500 Add quiz scoring ← the merge baseIf a merge goes badly and you want to start over: git merge --abort puts everything back to before the merge.
Gotchas
Resolving a conflict is a code change — test it before committing. Choosing "both" blindly can produce code that doesn't compile or logic that's wrong.
Conflicts are smaller when branches are short-lived. A branch that lives for three weeks collides with everything. Merge often.
Check yourself
4 questionsWhen does Git do a fast-forward merge?
Show answerHide answer
When the target branch hasn't moved since the other branch was created, so its pointer can just move forward.
What is a merge base?
Show answerHide answer
The most recent commit both branches share, used as the reference point in a three-way merge.
What do the <<<<<<<, ======= and >>>>>>> markers separate?
Show answerHide answer
Your current branch's version (between <<<<<<< and =======) and the incoming branch's version (between ======= and
>>>>>>>).
What are the steps to finish a merge after resolving conflicts?
Show answerHide answer
Edit the file to the correct content, remove the markers, git add it, then git commit.