Nishaglobal Education logo

Merging branches and fixing merge conflicts

Bring a branch's work into main with git merge, understand fast-forward and merge commits, and calmly resolve merge conflicts when two branches change the same lines.

Lesson 7 of 1140 minQuiz: 4 questions

🎯 By the end of this lesson you will be able to:

  • Merge a branch into main with git merge
  • Tell a fast-forward merge from a merge commit
  • Resolve a merge conflict, or abort a merge
Outline
  1. What you'll learn and why it matters
  2. A simple merge: fast-forward
  3. When both branches moved on: a merge commit
  4. Merge conflicts
  5. Resolving a conflict
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

What you'll learn and why it matters

Branches are only useful if their work can come back together. Merging combines the commits of one branch into another. Most merges happen automatically. Sometimes two branches change the same line, and Git asks you to decide: a merge conflict. Conflicts look scary the first time, but fixing them is just editing a file.

A simple merge: fast-forward

To merge, first switch to the branch you want to bring work into (usually main), then merge the other branch:

Terminal
git switch main
git merge contact-page
git log --oneline

Output

Already on 'main'
Updating 1ce7ed1..4970bdd
Fast-forward
 contact.html | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 contact.html
4970bdd (HEAD -> main, contact-page) Add contact page
1ce7ed1 Add intro sentence
ec605b9 Add stylesheet
6103896 Add home page

Fast-forward means main had no new commits of its own since the branch started, so Git simply moved the main label forward to the branch's latest commit. No new commit was needed.

When both branches moved on: a merge commit

If main also gained commits while you worked on the branch, Git combines both lines with a new merge commit. Here someone fixed a typo on main while the contact page was being built:

Terminal
printf '<h1>Asha Kumar</h1>\n<p>Junior web developer</p>\n<p>I build accessible web sites.</p>\n' > index.html
git commit -q -am "Fix wording on home page"
git merge contact-page
git log --oneline --graph

Output

Merge made by the 'ort' strategy.
 contact.html | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 contact.html
*   d500bb2 (HEAD -> main) Merge branch 'contact-page'
|\  
| * 4970bdd (contact-page) Add contact page
* | 436552c Fix wording on home page
|/  
* 1ce7ed1 Add intro sentence
* ec605b9 Add stylesheet
* 6103896 Add home page
1ce7mainFix wordingpageAdd contact pagemergeMerge commit: two parentsmain now has the wording fix and the contact page.
main and contact-page each have a commit the other does not. git merge creates a new merge commit on main with two parents, one from each branch, so main now contains both the wording fix and the contact page.

Normally Git opens your editor with the message *Merge branch 'contact-page'*; save and close it to finish. The graph shows the two lines joining at the merge commit.

Merge conflicts

A conflict happens when both branches changed the same lines of the same file differently. Git cannot know which version you want. Here main made the headings teal while a new-colours branch made them purple:

Terminal
git merge new-colours
git status --short

Output

Auto-merging styles.css
CONFLICT (content): Merge conflict in styles.css
Automatic merge failed; fix conflicts and then commit the result.
UU styles.css

UU means *unmerged*: the file needs your decision. Open it, and Git has written both versions into it, between conflict markers:

Terminal
git merge -q new-colours
cat styles.css

Output

Auto-merging styles.css
CONFLICT (content): Merge conflict in styles.css
Automatic merge failed; fix conflicts and then commit the result.
<<<<<<< HEAD
h1 { color: teal; }
=======
h1 { color: purple; }
>>>>>>> new-colours
MarkerMeans
<<<<<<< HEADStart of the version on your current branch (main)
=======The divider
>>>>>>> new-coloursEnd of the version from the branch you are merging

Resolving a conflict

  1. Open the file and decide what the final content should be: one version, the other, or a mix.
  2. Delete the three marker lines, leaving only the content you want.
  3. Save, then git add the file to mark the conflict as resolved.
  4. Run git commit to finish the merge.
Terminal
git merge -q new-colours
printf 'h1 { color: teal; }\n' > styles.css
git add styles.css
git commit -q -m "Merge new-colours, keep teal headings"
git status
git log --oneline --graph -4

Output

Auto-merging styles.css
CONFLICT (content): Merge conflict in styles.css
Automatic merge failed; fix conflicts and then commit the result.
On branch main
nothing to commit, working tree clean
*   e32821f (HEAD -> main) Merge new-colours, keep teal headings
|\  
| * 076f1dd (new-colours) Make headings purple
* | 86abc20 Make headings teal
|/  
* 1ce7ed1 Add intro sentence

Changed your mind in the middle of a conflict? git merge --abort puts everything back exactly as it was before the merge:

Terminal
git merge -q new-colours
git merge --abort
git status
cat styles.css

Output

Auto-merging styles.css
CONFLICT (content): Merge conflict in styles.css
Automatic merge failed; fix conflicts and then commit the result.
On branch main
nothing to commit, working tree clean
h1 { color: teal; }

Practice

Guided exercise

Finish a feature and clean up

Merge the contact-page branch into main, then delete the branch and list the branches.

  1. Make sure you are on main: git switch main.
  2. git merge contact-page
  3. git branch -d contact-page, then git branch.
Show the solution
Solution
git switch -q main
git merge -q contact-page
git branch -d contact-page
git branch

Output

Deleted branch contact-page (was 4970bdd).
* main

How to get there: After the merge, main contains the contact page, so -d deletes the branch safely. Only main is left. This merge, then delete cycle is what happens to almost every branch.

Your turn

Resolve by keeping both ideas

Merge new-colours into main, and resolve the conflict by keeping teal headings and adding a purple subheading rule, h2 { color: purple; }. Commit the result and show the file.

Show the solution
Solution
git merge -q new-colours
printf 'h1 { color: teal; }\nh2 { color: purple; }\n' > styles.css
git add styles.css
git commit -q -m "Merge new-colours: teal h1, purple h2"
cat styles.css

Output

Auto-merging styles.css
CONFLICT (content): Merge conflict in styles.css
Automatic merge failed; fix conflicts and then commit the result.
h1 { color: teal; }
h2 { color: purple; }

How to get there: Resolving is just writing the content you want, without markers. Here the final file mixes both ideas. git add marks it resolved and git commit completes the merge.

Common mistakes

Merging in the wrong direction

You see: Main's changes end up on your feature branch instead

Fix: Switch to the branch you want to update (usually main) first, then merge the other one into it.

Leaving conflict markers in the file

You see: Broken pages or code full of <<<<<<<

Fix: Remove every marker line before git add.

Panicking during a conflict

You see: Random commands make it worse

Fix: Read git status; finish the steps, or run git merge --abort to start again.

Forgetting to git add after editing

You see: git commit says the conflict is not resolved

Fix: Stage each resolved file, then commit.

Recap

  • Switch to the receiving branch, then git merge other-branch.
  • Fast-forward just moves the label; a merge commit joins two lines of work.
  • A conflict means both sides changed the same lines; Git writes both between markers.
  • Resolve: edit, remove markers, git add, git commit; or git merge --abort.

Key terms

Merge
Combine the changes from one branch into another.
Fast-forward
A merge where the branch label simply moves forward because there is nothing to combine.
Merge conflict
When two branches change the same lines differently and Git needs you to choose.

Check your understanding

Answer all 4 questions, then check your answers to see an explanation for each one.

  1. 1. You want main to include the work from the menu branch. What do you run?

  2. 2. What does a fast-forward merge do?

  3. 3. What does the ======= line mean in a conflicted file?

  4. 4. How do you cancel a merge that has conflicts?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.