[Git Basics] Mastering Git Merge with Detailed Guide and Examples

VnnTools

Have you ever felt a cold sweat when the red CONFLICT message appeared in your terminal? Or have you ever been confused about how to safely merge your code into a shared project? If so, you’re in the right place.

Mastering Git Merge with Detailed Guide and Examples

git merge is one of the most basic yet powerful commands in Git, the "heart" of every team workflow. Mastering it not only boosts your confidence but also your productivity, turning you from a "Git user" into a "Git pro".

This article will take you from the most fundamental concepts, through common merging techniques, and equip you with the tools to handle merge conflicts calmly and professionally. 🚀

What is Git Merge? Why is it important?

Imagine you and a colleague are writing a book together. You write chapter 1 on one draft, and your colleague writes chapter 2 on another. When both are done, merge is the act of combining those drafts into a complete book.

In Git, git merge is the command used to integrate changes from one branch into another. Typically, you’ll merge a feature branch into the main branch of your project (like main or develop).

Why is it crucial?

  • Seamless collaboration: Allows multiple people to work on different features in parallel without stepping on each other’s toes.
  • Feature integration: The standard way to bring a finished feature into the main product.
  • Version management: Maintains a clear development history, recording important project milestones.

Two Classic Merge Types: Fast-Forward and Three-Way

Git is smart and will automatically choose one of two merging strategies depending on the commit history of the branches.

1. Fast-Forward Merge ⏩

This is the simplest and "happiest" scenario. It happens when the branch you want to merge into (main) has no new commits since you created your feature branch.

  • Imagine: You take a side road to fix your car (feature branch). Meanwhile, no new cars pass on the main road (main). When you’re done, you just drive straight back onto the main road.
  • How it works: Git simply moves the pointer of the main branch to the last commit of the feature branch. The commit history remains a straight, clean line.
# Assume you are on the feature branch and have finished committing
git checkout main      # Switch to the main branch
git pull               # Make sure main is up to date
git merge feature/new-login # Merge the feature branch in

If you see the "Fast-forward" message, congratulations, you’ve just made a perfect merge!

2. Three-Way Merge 💎

This is more common in real life. It happens when both branches (main and feature) have new commits since they diverged.

  • Imagine: This time, while you’re on the side road fixing your car, several new cars have passed on the main road. You can’t just drive straight in anymore. Instead, you need to create a new "junction" to connect your road to the main one.

  • How it works: Git finds 3 points:

    1. The most recent common ancestor commit of both branches.
    2. The latest commit on the current branch (main).
    3. The latest commit on the branch to be merged (feature).

Then, Git automatically creates a merge commit to combine the changes from both branches. This commit has two parents, forming a diamond shape in the commit history.

The Nightmare Called "Merge Conflict": How to Resolve 😱

This is the scariest part for beginners, but don’t worry—once you understand it, it’s not so bad.

Why do conflicts happen?

Conflicts occur when Git can’t automatically decide which changes to keep. This usually happens when both branches edit the same part of a file. Git gets "confused" and needs human intervention.

4-Step Conflict Resolution Process ✅

When a conflict occurs, Git will notify you and mark the conflicted files. git status will show "Unmerged paths". Stay calm and follow these steps:

Step 1: Identify the conflict area

Open the conflicted file in your code editor (like VS Code). You’ll see specially marked code blocks:

<<<<<<< HEAD
// Code from your current branch (e.g., main)
console.log("Hello World");
=======
// Code from the branch being merged in (e.g., feature/new-login)
console.log("Hello Git");
>>>>>>> feature/new-login
  • <<<<<<< HEAD: Start of code from your current branch.
  • =======: Separator between the two versions.
  • >>>>>>> feature/new-login: End of code from the branch being merged.

Step 2: Make a decision

Now you, as the developer, must decide which version to keep:

  • Keep your code (HEAD).
  • Keep the other branch’s code.
  • Combine both.
  • Or write a completely new version.

Step 3: Clean up the markers

After deciding, delete all the marker lines (<<<<<<<, =======, >>>>>>>) and save the file. Your file should now only contain the final code you want.

For example, if you decide to combine both:

// Final code after resolving the conflict
console.log('Hello World and Hello Git')

Step 4: Complete the Merge

Go back to the terminal and tell Git you’ve resolved the conflict:

# Add the resolved file to the staging area
git add <filename>

# Commit to complete the merge
git commit

Git will automatically create a default commit message like "Merge branch 'feature/new-login' into main". Just save it and you’re done!

💡 Pro tip: Most modern IDEs like VS Code have a very intuitive graphical interface for resolving conflicts, letting you "Accept Current Change", "Accept Incoming Change", or "Accept Both Changes" with a single click.

Merge vs. Rebase: The Classic Showdown

Once you’ve mastered merge, you’ll hear about another concept: rebase. So what’s the difference, and when should you use each?

Git Merge vs. Rebase

Criteriagit mergegit rebase
PurposeCombine two branches together.Rewrite the commit history of one branch onto another.
HistoryPreserves original history, creates a merge commit. History can branch.Creates a linear (straight) history. It rewrites commits.
SafetyVery safe, does not change existing commits.Dangerous if used on shared (public) branches because it rewrites history.

Golden rules:

  • Use merge when you want to integrate changes from a shared branch (like develop, main) into your personal branch, or when you want to merge a feature branch into the main branch and keep a clear merge history.
  • Use rebase on your personal branch before merging into the main branch to clean up history, making it straight and easy to follow. Never rebase a branch that’s already been pushed to remote and used by others.

Conclusion: Git Merge Isn’t Scary

git merge really isn’t scary—it’s a fantastic collaboration tool and the foundation of countless successful software projects. By understanding the types of merge, practicing conflict resolution, and knowing when to use merge versus rebase, you’re well on your way to becoming a professional developer.

Open your terminal, create some test branches, and practice today. The more conflicts you face, the more confident and quick you’ll become at resolving them.

Good luck mastering Git and making it your trusty sidekick in every project!

Related Posts

[Git Basics] What is Git? Discover the Role of Git in Programming

Git is an extremely popular version control system. This article explains what Git is, why every developer should know it, and the steps to get started.

[Git Basics] What is the .gitignore File? How to Use It Effectively for Every Project

Learn how to use the .gitignore file to exclude temporary files, logs, and unnecessary folders. Simplify your project and keep your Git repository clean.

[Git Basics] Git Workflow: A Detailed Guide to Popular Team Collaboration Models

A guide to applying top Git Workflows. Master Git Flow, GitHub Flow, and GitLab Flow to optimize your development process, ensure code quality, and accelerate your project.

[Git Basics] Understanding the File Life Cycle in Git: 3 States You Need to Know

The life cycle of a file in Git is not as complicated as you might think. This article will help you master the 3 main states and how they work to manage your source code more effectively.