Please enable JavaScript to use CodeHS

Agentic Engineering & AI Software Topics

Merging and conflicts

Merging brings the work from one branch into another. Git can usually do it automatically, but when two branches change the same place, a merge conflict asks a human to decide which version to keep.

Git ~12 min High School / College Prep
The Big Idea

Why merge?

Branches are useful because they isolate work, but eventually you want the new work to rejoin the main line. Merging is Git's way of combining two histories into one.

The most common command is git merge feature run while you are on main. Git looks at the commits on feature that are not yet on main and adds them to the history.

Workflows

Fast-forward vs merge commit

When main has not moved since the branch was created, Git can simply slide main forward to the tip of the feature branch. This is a fast-forward merge. The history stays perfectly straight.

When both branches have new commits, Git creates a merge commit. A merge commit has two parents: one from the main line and one from the branch. It is a visible record that two streams of work came together.

Tip: Some teams avoid merge commits and use rebase instead, which we cover in the next tutorial. Neither is always right; the choice depends on the team's workflow.

Code

When branches diverge

Below, the same index.html file was edited differently on main and on feature. These are the kinds of changes that can produce a conflict when they touch the same lines.

Pattern

Merge conflicts

A merge conflict happens when the same part of a file was changed in both branches and Git cannot decide which version is correct. It stops and writes conflict markers into the file:

Conflict markers in index.html
<<<<<<< HEAD
  <h1>Hi</h1>
=======
  <h1>Hello World</h1>
>>>>>>> feature

Your job is to edit the file, keep the version you want, and remove the markers. Then you stage the file and commit the merge.

Merge conflicts are not failures. They are a pause button that prevents one person's work from silently overwriting another's.

Interactive

Resolve a conflict

Walk through a merge that hits a conflict. Use the buttons to advance, then choose how to resolve the <h1> line.

index.html

<!DOCTYPE html>
<html>
<head><title>Site</title></head>
<body>
  <h1>Site</h1>
</body>
</html>
Ready. Start the merge scenario.
Resolved. The file now contains the version you chose. In a real workflow you would run git add index.html and git commit to finish the merge.
Your Turn

Check yourself

What is the difference between a fast-forward merge and a merge commit?

Your answer:

Sample answer: A fast-forward merge just moves the branch pointer forward because there is no divergence. A merge commit is created when both branches have new commits and the history needs to record the join.

Check Yourself

Quick quiz

1. When does Git create a merge commit?

2. What does a merge conflict mean?

3. After resolving a conflict, what is the next step?

Keep learning

You know how to merge and resolve conflicts. Next, learn how to rewrite history with rebase to keep a clean, readable commit graph.

Part of the “Agentic Engineering & AI Software Topics” series.