Git: resolving a conflict

When Git cannot merge on its own: the same lines changed on both sides, conflict markers, resolving, staging, concluding the merge or abandoning it with merge --abort.

  • 20 cards
  • 5 topics
  • Free

The outline, topic by topic

Each topic is weighted the way the exam weighs it.

  1. Concepts
  2. Questions
  3. Sort and link
  4. Scenarios
  5. Understand

A few cards from the deck

  • Merge conflict: what is it?

    Show the answer

    A situation where two branches changed the same lines of a file, or one deleted what the other changed: Git cannot choose on its own.

    Git compares each side with the common ancestor. If the changes do not overlap, it merges on its own; otherwise it stops and asks you to decide.

  • You are on branch main and run git merge menu. What does the block between <<<<<<< HEAD and ======= represent?

    ```text
    <<<<<<< HEAD
    Salut
    =======
    Hello
    >>>>>>> menu
    `

    • The version from branch main, the one you are on
    • The version from branch menu, which you are merging
    • The version from the common ancestor of the two branches
    • The version on the remote repository, before your last commit
    Show the answer

    The version from branch main, the one you are on

    The top is what HEAD, hence your current branch, contains; the bottom is what the merged branch brings. The common ancestor is not shown by default.

  • In a conflicted file, the block between the opening marker and the separator is the version from the … branch, and the one between the separator and the closing marker is from the … branch.

    Show the answer

    In a conflicted file, the block between the opening marker and the separator is the version from the current branch, and the one between the separator and the closing marker is from the merged branch.

    The opening marker carries HEAD, which is the branch you are on; the closing marker carries the name of the branch you are merging.

  • Conflict markers: what is it?

    Show the answer

    Lines Git writes into the file to delimit the two versions: an opening, a separator and a closing.

    They open with seven less-than signs and HEAD, split with seven equal signs, close with seven greater-than signs and the other branch's name. They must never be committed.

  • A conflict on t.txt is beyond you and you want to go back to the state before the merge. What do you do?

    • git switch main to leave the merge
    • git commit -am "conflict" to resume later
    • git branch -d menu to delete the faulty branch
    • git merge --abort
    Show the answer

    git merge --abort

    git merge --abort cancels the merge without losing anything. git switch is refused during a merge (“cannot switch branch while merging”), committing would record the markers, and deleting the branch settles nothing.