🔥 0
0
Lesson 8 of 10 25 min +150 XP

Merging Branches

Once you've finished working on a feature branch, you'll want to bring those changes back into your main branch. This is called merging.

Basic Merge

To merge a branch into your current branch:

# First, switch to the branch you want to merge INTO
git switch main

# Then merge the other branch
git merge feature-login

Types of Merges

Fast-Forward Merge

If the main branch hasn't changed since you created your feature branch, Git simply moves the pointer forward:

Before:
main ──○──○──○
              └──○──○ feature

After (fast-forward):
main ──○──○──○──○──○
                    ▲
              (feature merged)

No new commit is created - Git just moves the main pointer.

3-Way Merge

If both branches have new commits, Git creates a merge commit that combines them:

Before:
main ──○──○──○──○
              └──○──○ feature

After (3-way merge):
main ──○──○──○──○──────○ (merge commit)
              └──○──○──┘

The merge commit has two parents - one from each branch.

Merge Conflicts

Sometimes Git can't automatically merge because both branches changed the same lines. This is a merge conflict.

When this happens, Git marks the conflicting sections in your files:

<<<<<<< HEAD
This is the version from main
=======
This is the version from feature
>>>>>>> feature

You must:

  • Edit the file to resolve the conflict
  • Remove the conflict markers
  • Stage and commit the resolved file

Try It Out!

Use the merge simulator to see both types of merges in action.

Visual Mode: Drag one branch onto another to merge Command Mode: Use git merge

Try creating scenarios that result in fast-forward and 3-way merges!

Branching Basics