Lesson 01 of 05Concept

What is Merging?

Goal: Understand how Git brings the work of two branches together.

Bringing work back together

Branches let work happen in isolation. Merging is how that work comes back: it takes the commits from another branch and joins them into the branch you are on.

You always merge *into* your current branch. To bring feature/login into main, you switch to main and run git merge feature/login.

Fast-forward: nothing to combine

Step 1 of 2

feature/login has two new commits. main has not moved since the branch was created.

Step 1 of 2. `feature/login` has two new commits. `main` has not moved since the branch was created.

Three-way merge: both branches moved

Step 1 of 2

This time main got a commit of its own after feature/login started. The history has split into two lines.

Step 1 of 2. This time `main` got a commit of its own after `feature/login` started. The history has split into two lines.

Why "three-way"?

To combine the branches, Git compares three snapshots:

  • the merge base, the last commit both branches share (here Add homepage),
  • ours, the tip of the branch you are on,
  • theirs, the tip of the branch you are merging in.

Whatever changed on only one side since the merge base is taken from that side. That is why two people can edit different files, or different parts of one file, without any trouble.

Finished reading?

Marking it complete records your progress and XP for this lesson.