Merging, and What a Conflict Really Is

Merging. You switch back to the branch that should receive the work, then merge the other one into it.

`

git switch main

git merge add-contact-page

`

When main has not moved since you branched, git simply moves its pointer forward. That is a fast-forward, and it is the common case.

✦ A conflict is a question, not a failure

A conflict happens when the same lines changed on both branches, so git cannot know which version is right. It is not an error and nothing is broken. Git is asking you a question it genuinely cannot answer, because deciding which change is correct requires understanding what the code means. You open the file, choose, remove the markers, then add and commit.

What a conflict looks like.

`

<<<<<<< HEAD

<h1>Amara Okafor</h1>

=======

<h1>Amara Okafor, Developer</h1>

>>>>>>> add-title

`

Above the equals signs is what is on your current branch. Below is what is coming in. You delete the markers and leave the version you want, which may be a combination of both.

How to avoid most conflicts. Merge often and keep branches short lived. A branch open for a day conflicts rarely. A branch open for three weeks, while five other people edit the same files, conflicts constantly. The fix is a workflow habit rather than a git command.

SANKOFA SANDBOX
learner:~$

This sandbox performs fast-forward merges. Real conflicts need two people editing the same lines, which is a scenario for your own machine.

Try it → Init and commit, then `git switch -c add-notes`, create a file with `echo notes > notes.md`, `git add notes.md`, commit it. Then `git switch main`, run `ls` and notice notes.md is not there. Then `git merge add-notes` and `ls` again.

◈ Graded Task

Do a piece of work on a branch and bring it home. In the portfolio, initialize and commit everything. Then create and switch to a branch called add-contact, add a file called contact.html containing a heading, commit it on that branch, switch back to main, and merge the branch in.

Checked against the filesystem you leave behind, not the commands you type, so any route that gets the job done counts. 5 visible and 2 hidden checks.

SANKOFA SANDBOX
learner:~$

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back