New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› Git› Merging and Resolving Conflicts

Merging and Resolving Conflicts

💻 Git

Merge branches, understand fast-forward versus three-way merges and fix conflicts calmly.

Lesson 6 of 9 Tutorial
0 of 9 completed

A conflict happens when two branches change the same lines. Git stops and asks you to decide, which is safer than guessing.

By the end of this lesson you can:
  • Merge a branch.
  • Read conflict markers.
  • Resolve and complete a merge.

Merging

Switch to the branch that should receive the work, then merge the other branch into it.

git switch main
git merge feature/about-page

Fast-Forward vs Three-Way

If main has no new commits, Git simply moves the pointer forward. If both branches moved, Git creates a merge commit with two parents.

Resolving a Conflict

Git marks the conflicting section. Edit the file to the version you want, remove the markers, then stage and commit.

# after editing the file
git add index.html
git commit -m "Merge feature/about-page and resolve heading conflict"

Output

<<<<<<< HEAD
<h1>Welcome to our site</h1>
=======
<h1>Welcome to Courselord</h1>
>>>>>>> feature/about-page

Abort if Needed

If a merge gets confusing, return to the state before it started.

git merge --abort
Tip Resolve conflicts by understanding both changes. Deleting one side blindly is how features disappear.

Practice

  1. Create a conflict on purpose in a test repository and resolve it.
  2. Explain the difference between fast-forward and three-way merges.
  3. Abort a merge and confirm the working directory is clean.

Hands-on exercise

1. On main, change a line in README.md and commit.
2. Create a branch, change the same line differently and commit.
3. Merge the branch into main.
4. Resolve the conflict so both ideas are represented, then commit.

Recap

Merging combines lines of work. Conflicts are normal; read both sides, edit deliberately and commit the resolution.

Example

git switch main
git merge feature/about-page

Exercises

Exercise 1. Resolve a conflict Medium
1. On main, change a line in README.md and commit.
2. Create a branch, change the same line differently and commit.
3. Merge the branch into main.
4. Resolve the conflict so both ideas are represented, then commit.
Git exercise-1.txt
Expected Output
A merge commit with a README that reflects a deliberate decision.

Lesson Nav

Lesson sections