Arc 2 Quest 07

The Conflict of Kingdoms

Merge, conflicts and resolution

Introduction

The archive room trembles slightly. On the great oak table, two scrolls are unrolled side by side - two chronicles of the same event, but with contradictory accounts. The Master Archivist places a hand on each scroll, his gaze grave.

"Apprentice, you have learned to open parallel timelines - branches. But what happens when those timelines must converge? When two kingdoms tell the same battle, but with different versions? That is where the merge comes in - the fusion of chronicles. Sometimes, the merge happens naturally. Sometimes, the accounts contradict each other, and it takes an Archivist to reconcile the versions. That is your mission today."

What is a merge?

A merge (fusion) is the operation that reunites two branches into one. It is like gathering the chronicles of two archivists who worked in parallel on different subjects - or sometimes on the same one.

Imagine two chroniclers sent to different kingdoms:

  • One heads East and records the history of the Kingdom of the Dawn.
  • The other heads West and records the history of the Kingdom of the Dusk.

When they return, their chronicles must be merged into a single great register. If each wrote about different subjects, it is simple. But if they wrote about the same battle with contradictory versions... there is a conflict.

Merging a branch: git merge

To merge a branch into the current branch:

git merge <branch>

Important: you must first be on the branch that receives the changes. Typically, you switch to main before merging a working branch:

git switch main
git merge my-branch

This takes the commits from my-branch and integrates them into main.

Types of merge

The fast-forward - The simple merge

When the target branch (main) has received no new commits since the working branch was created, Git can simply "advance" the main pointer to the last commit of the branch. This is the fast-forward.

Before:
main:        A --- B
                    \
my-branch:           C --- D

After git merge my-branch (from main):
main:        A --- B --- C --- D

No merge commit is created - Git simply moves main forward. This is the simplest case.

The three-way merge

When both branches have evolved since their point of divergence, Git cannot simply advance a pointer. It must combine the changes from both sides. If the changes affect different files or different lines, Git handles it on its own and creates a merge commit:

Before:
main:        A --- B --- E
                    \
my-branch:           C --- D

After git merge my-branch (from main):
main:        A --- B --- E --- M
                    \         /
my-branch:           C --- D

Commit M is a merge commit - it has two parents (E and D). Git created it automatically because the changes did not overlap.

Merge conflicts

When two branches have modified the same lines of the same file, Git cannot decide on its own which version to keep. It stops and asks you to make the call. This is a merge conflict.

Anatomy of a conflict

When a conflict occurs, Git modifies the affected file by inserting conflict markers:

<<<<<<< HEAD
King Aldric won the battle thanks to his cavalry.
=======
King Aldric lost the battle against the enemy archers.
>>>>>>> chroniques-est

Here is what these markers mean:

  • <<<<<<< HEAD: start of your version (the current branch)
  • =======: separator between the two versions
  • >>>>>>> chroniques-est: end of the other branch's version

The conflict markers must all be removed before you can commit. If you leave even a single one, your file will be corrupted and Git will not consider the conflict resolved.

Resolving a conflict

To resolve a conflict, follow these steps:

  1. Open the file containing the conflict in your editor
  2. Choose which version to keep, or combine both versions into a coherent narrative
  3. Remove all conflict markers (<<<<<<<, =======, >>>>>>>)
  4. Save the file
  5. Mark the conflict as resolved with git add
  6. Create the merge commit with git commit
# After manually editing the file:
git add chroniques.txt
git commit -m "Merge chroniques-est: reconcile the battle accounts"

Abort a merge in progress

If you feel overwhelmed by a conflict and want to go back:

git merge --abort

This aborts the merge and restores everything to the state before. You can try again later when you are ready.

git merge --abort is your safety net. Do not hesitate to use it if you got tangled up resolving a conflict - it is better to start fresh than to commit a poorly resolved file.

Tips to avoid conflicts

  1. Merge frequently: the longer you wait, the more branches diverge, and the more numerous and complex the conflicts become.
  2. Communicate with your team: if someone is working on the same file as you, coordinate.
  3. Split your files well: a 1000-line file creates more conflicts than a dozen well-organized files.
  4. Keep your branches short-lived: a branch that lives 2 days creates fewer conflicts than one that lives 2 months.

Practical exercise - The Conflict of Kingdoms

Two chroniclers have been sent to the borders of the kingdom. Elowen went East, to the Kingdom of the Dawn. Gareth went West, to the Kingdom of the Dusk. Each recorded their version of the Battle of the Stone Bridge. Unfortunately, their accounts contradict each other. You must reconcile their chronicles.

Step 1 - Retrieve the archive

git clone archive.bundle le-conflit-des-royaumes
cd le-conflit-des-royaumes

Step 2 - Explore the branches

git branch -a
git log --oneline --all --graph

You should see the main branch and the remote branches origin/chroniques-est and origin/chroniques-ouest. Since the repository was cloned from a bundle, the chroniclers' branches are remote branches - they are referenced with the origin/ prefix.

Step 3 - First merge (no conflict)

First, merge the origin/chroniques-ouest branch into main:

git switch main
git merge origin/chroniques-ouest

This merge should go smoothly - Gareth added his report and wrote his version of the battle. Since main has also evolved on its own, Git will automatically create a merge commit.

Check the result with git log --oneline --graph. You should see both branches joining in a merge commit.

Step 4 - Second merge (with conflict!)

Now, merge the origin/chroniques-est branch:

git merge origin/chroniques-est

This time, Git stops with a conflict message! Elowen and Gareth wrote different versions of the same section in chroniques.txt. Git cannot choose on its own.

Don't panic! This is normal and this is exactly what we are going to learn to handle. The file rapport-est.txt was merged without issue (no conflict on it), only chroniques.txt is problematic.

Step 5 - Resolve the conflict

  1. Open chroniques.txt in your text editor
  2. Look for the markers <<<<<<<, ======= and >>>>>>>
  3. Read both versions of the battle's account
  4. Write a narrative that combines both perspectives into a coherent and balanced version - as a good Archivist, you do not take sides!
  5. Remove all conflict markers
  6. Save the file

There is no single right answer! What matters is that the result is coherent, that both perspectives are represented, and that no conflict markers remain in the file.

Step 6 - Finalize the merge

git add chroniques.txt
git commit -m "Merge chroniques-est: reconcile the battle accounts"

Step 7 - Verify

Run the verification script to validate your quest:

Bash (Linux / macOS / Git Bash on Windows):

bash verifier.sh

PowerShell (Windows):

.\verifier.ps1

The script checks:

  1. You are in a Git repository
  2. A merge has been performed (merge commit present)
  3. No conflict markers remain in the files
  4. No merge is in progress (no unresolved conflict)
  5. The merge commit has a descriptive message (at least 10 characters)

Command summary

Command Description
git merge <branch> Merge the specified branch into the current branch
git merge --abort Abort a merge in progress and return to the previous state
git log --oneline --graph Visualize the history with branches and merges
git add <file> Mark a conflicted file as resolved
git commit Finalize the merge commit after resolving conflicts

The Master Archivist examines the unified register, where both versions of the battle now coexist in a coherent narrative.

"You have accomplished something remarkable, Apprentice. Reconciling contradictory versions is one of the most delicate tasks of an Archivist. In the real world, conflicts are inevitable when multiple people work together. What matters is knowing how to resolve them with care and discernment. You have now mastered the art of merging."