Trending
| 5:14 AM IST
Git Merge and Merge Conflicts: How to Combine Branches Calmly (Part 13)
Tech

Git Merge and Merge Conflicts: How to Combine Branches Calmly (Part 13)

Merging brings a branch's work back into main. Learn fast-forward and three-way merges, read conflict markers, and resolve a merge conflict step by step in VS Code. Part 13 of our Git series.

By · 9 min read 2 views Rate it 0 comments

To merge a branch, switch to the branch you want to update, usually main, and run git merge feature-branch. Git combines the work automatically. If both branches changed the same lines, Git stops with a merge conflict. You then choose which version to keep, remove the conflict markers, stage the file and commit.

The cake booking system was finally ready. Sneha switched to main, typed git merge feature/cake-booking, pressed Enter and saw a word that made her stomach drop: CONFLICT. While she built the booking page, she had also changed the homepage heading on main for Diwali, and her branch had changed the same heading differently. She was sure she had broken everything. She had not. A merge conflict is not an error; it is Git politely asking a question it cannot answer alone. In Part 12 you built work on a branch while main stayed safe. This Part 13 shows how to bring it home, why conflicts happen and how to resolve them calmly.

Your one-minute summary

  • git switch main then git merge feature/cake-booking
  • Fast-forward merge: main simply moves ahead, no extra commit
  • Three-way merge: Git creates a merge commit joining both lines
  • A conflict happens when both branches changed the same lines
  • Resolve, then git add and git commit; or git merge --abort to back out
  • Small, short-lived branches mean fewer conflicts

How do I merge a branch into main?

Merging always happens "into" the branch you are currently on. So first move to main, update it, then merge your feature branch into it.

Merging a feature branch

git switch mainGo to the branch that will receive the work
git pullMake sure main is up to date
git merge feature/cake-bookingBring the feature's commits into main
Test the siteCheck that everything still works together
git push, then delete the branchgit branch -d feature/cake-booking

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

It depends on whether main changed while you worked on your branch. The Pro Git book describes both cases in detail.

Two kinds of merge

Fast-forward

  • main had no new commits since you branched
  • Git just moves main forward to your latest commit
  • No extra commit; history stays a straight line
  • Message: "Fast-forward"

Three-way merge

  • main and your branch both have new commits
  • Git compares both with their common ancestor
  • Creates a "merge commit" with two parents
  • Message mentions the merge strategy

Either way, if the changes touch different lines, Git merges them automatically.

Also readYour First Git Repository and First Commit, Step by Step (Part 7)

Why do merge conflicts happen?

A conflict happens when both branches changed the same part of the same file in different ways, and Git cannot know which version is right. In Sneha's case:

  • On main, she changed the homepage heading to "Happy Diwali from Sri Lakshmi Bakery".
  • On her branch, she had changed the same heading to "Order your custom cake online".
  • When she merged, Git saw two different edits on one line and asked her to decide.

Changes to different files, or to different parts of the same file, merge cleanly. Conflicts are about overlapping edits only.

When does Git need your help?

Different files changedMerged automatically
Same file, different linesMerged automatically
Same file, same lines, different editsConflict: Git asks you to decide

What do conflict markers mean?

When a conflict happens, Git writes both versions into the file, separated by markers. Here is what Sneha saw in index.html:

Line in the fileMeaning
<<<<<<< HEADStart of the version on your current branch (main)
<h1>Happy Diwali from Sri Lakshmi Bakery</h1>main's version
=======Divider between the two versions
<h1>Order your custom cake online</h1>The incoming branch's version
>>>>>>> feature/cake-bookingEnd of the incoming version
Nothing is lostBoth versions are right there in the file. Your job is to edit the file so it contains the final version you want, and delete the three marker lines. You can keep one side, the other, or a mix. Sneha kept both: "Happy Diwali! Order your custom cake online".

How do I resolve a merge conflict, step by step?

Resolving a conflict

1. Read git statusFiles with conflicts are listed under "Unmerged paths: both modified"
2. Open each conflicted fileSearch for <<<<<<< to find every conflict
3. Decide the final contentKeep one version, the other or combine them; delete all markers
4. Save and stagegit add index.html marks the conflict as resolved
5. Finish the mergegit commit creates the merge commit; test the site, then push

Changed your mind halfway? git merge --abort returns everything to how it was before the merge.

How does VS Code help with conflicts?

VS Code highlights conflicts in colour and adds clickable options right above each one:

  • Accept Current Change: keep main's version.
  • Accept Incoming Change: keep the branch's version.
  • Accept Both Changes: keep both, one after the other, then edit as needed.
  • Compare Changes: view the two side by side.

VS Code also offers a three-panel merge editor ("Resolve in Merge Editor") that shows each side and the result. After resolving, click + to stage the file and then Commit.

GitHub Desktop detects conflicts too and offers to open the file in your editor; once resolved, it shows a "Continue merge" button. On GitHub.com, simple conflicts in a pull request can be resolved in the browser, as described in GitHub's guide to resolving a merge conflict on GitHub.

How can I avoid merge conflicts?

Fewer conflicts, less stress

  • Pull main before you create a branch
  • Keep branches small and merge within days, not weeks
  • Merge or pull main into your branch regularly if it lives longer
  • Agree with teammates who is editing which files
  • Avoid reformatting whole files in a feature branch
  • Commit and push often so others see your changes early

What about rebase?

You will hear about git rebase, another way to combine branches by replaying your commits on top of main for a straight-line history. It is useful but rewrites commits, so it carries the same warning as Part 11: never rebase commits others have already pulled. For beginners, git merge is the safer default, and everything in this series works without rebase.

Merge cheat sheet

git merge branchBring branch into the current one
git merge --abortCancel a merge in progress
git add fileMark a conflict as resolved
git log --graphSee how branches joined

Before Part 14, make sure you can

  • Merge a feature branch into main
  • Explain fast-forward vs three-way merge
  • Read the three conflict markers
  • Resolve a conflict in VS Code and finish with a commit
  • Cancel a merge with git merge --abort

Her View

The first merge conflict often feels like an alarm going off. It is not. It is a conversation: two good ideas met in the same place, and Git is asking you to choose. Approach it calmly, read both versions and decide what serves the project best. Often, as Sneha found, the best answer combines the two.

Also readHow to Install Git, VS Code and GitHub Desktop on Windows and Mac (Part 5)

His Insight

Conflicts are a symptom of branches drifting apart. The cure is small, frequent merges. If a conflict looks complicated, abort, pull main into your branch, resolve there with time to test, and then merge into main. Always test the site after a merge before pushing, because code can merge "cleanly" and still not work together.

H View Verdict

Merging is how branch work becomes real, and conflicts are a normal, manageable part of it. With VS Code's buttons and a clear process, you can resolve most conflicts in minutes. In Part 14, we move the merge to GitHub with pull requests, where others can review your work before it reaches main.

Self-assessment: test yourself

Ready? Tap an option for each question. You will see right away whether you got it, with a short explanation, and your score at the end. Take your time, and try again as often as you like.

Merge quiz: 10 questions

1. To merge feature/cake-booking into main, you first

  1. Switch to feature/cake-booking
  2. Switch to main
  3. Delete main
  4. Run git clone

Merging happens into the branch you are on, so switch to main first.

2. When does a merge conflict happen?

  1. Every merge
  2. When files are new
  3. When both branches changed the same lines
  4. When you forget to push

Overlapping edits need a human decision.

3. What does the ======= line mean?

  1. End of file
  2. The divider between the two versions
  3. A syntax error
  4. A comment

It separates your version (above) from the incoming version (below).

4. After editing a conflicted file, what marks it as resolved?

  1. git push
  2. git add file
  3. git branch
  4. Closing VS Code

Staging the file tells Git the conflict is resolved; then commit.

5. Which command cancels a merge in progress?

  1. git reset --hard
  2. git merge --abort
  3. git revert
  4. git stash pop

--abort restores the state before the merge started.

6. A fast-forward merge happens when

  1. Both branches changed the same line
  2. main had no new commits since the branch was created
  3. You use --force
  4. The branch is deleted

Git simply moves main forward; no merge commit is needed.

7. In VS Code, which button keeps main's version?

  1. Accept Current Change
  2. Accept Incoming Change
  3. Compare Changes
  4. Discard

"Current" is the branch you are on, here main.

8. Two branches changed different files. What happens on merge?

  1. Always a conflict
  2. Git merges them automatically
  3. Git deletes one
  4. The merge fails

Only overlapping edits need your help.

9. Why test the site after a clean merge?

  1. Git requires it
  2. Changes can merge as text yet clash in behaviour
  3. To earn GitHub points
  4. It is not needed

Git merges text, not meaning; testing catches logical clashes.

10. Which habit reduces conflicts the most?

  1. Huge branches merged monthly
  2. Small branches merged often
  3. Never pulling main
  4. Reformatting every file

Small, frequent merges keep branches close to main.

Frequently asked questions

Are merge conflicts bad?

No. They are normal when people, or one person on two branches, edit the same lines. They simply need a decision.

Can I merge main into my feature branch?

Yes, and it is a good habit for longer branches. On your branch, run git merge main to bring in the latest work and resolve any conflicts early.

Why did my merge succeed but the site broke?

Git merges text, not meaning. Two changes can merge cleanly but clash in behaviour, such as two CSS rules for the same button. Always test after merging.

Should I delete branches after merging?

Yes. Once merged, the commits live in main. Delete the branch locally with git branch -d and on GitHub with the "Delete branch" button.

Part 13 of 20 in our Git and GitHub for Beginners series. A small tip from us: create a conflict on purpose in your practice repository. Resolving one when nothing is at stake makes the real ones painless. Next up, Part 14: Pull requests and code review on GitHub.
Topics: Tech
Harika Behara

Writes for H View, where every story follows our editorial policy and is reviewed before it is published.

All stories by Harika →

H View stories follow our editorial policy. Spotted an error? Tell us and we will correct it.

Rate this article

One tap is enough. No comment needed.

Your rating
Be the first to rate it.

Was this article helpful?

Your answer goes straight to our editors.

Comments0

Be kind and stay on topic. Comments with links are checked before they appear.

No comments yet. Start the conversation.

The H View Brief

Our best stories, in your inbox.

The week’s best Her View, His Insight reading in one short, useful note.

Free. We only use your email to send The H View Brief. Unsubscribe any time.