Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Git

    Merging and Conflicts

    Merging combines the changes from one branch into another: git merge bonus (run while on main) brings bonus's

    Chapter 5 of 114 min read

    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 on main) brings bonus's work into main.

    • Fast-forward merge: if main hasn't moved since bonus branched off, Git just slides main'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").

    Output
    $ 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:

    Output
    def score(correct):
    <<<<<<< HEAD
        return correct * 20
    =======
        return correct * 10 + 5
    >>>>>>> bonus
    • Between <<<<<<< HEAD and =======: 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:

    Python
    def score(correct):
        return correct * 20 + 5
    Terminal
    git add quiz.py                  # "I've resolved this file"
    git commit --no-edit             # finish the merge commit with the default message

    The resulting history, from git log --oneline --graph --all:

    Output
    *   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 base

    If 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 questions
    1

    When does Git do a fast-forward merge?

    Show answer

    When the target branch hasn't moved since the other branch was created, so its pointer can just move forward.

    2

    What is a merge base?

    Show answer

    The most recent commit both branches share, used as the reference point in a three-way merge.

    3

    What do the <<<<<<<, ======= and >>>>>>> markers separate?

    Show answer

    Your current branch's version (between <<<<<<< and =======) and the incoming branch's version (between ======= and >>>>>>>).

    4

    What are the steps to finish a merge after resolving conflicts?

    Show answer

    Edit the file to the correct content, remove the markers, git add it, then git commit.