0%

Chapter 6

Merging and Conflicts

Bring Two Branches Together

Two can hold useful work that belongs together. One person may have improved prices while another added a new pricing tier. Keeping both branches forever leaves the project split. A merge combines their work into one line of history.

Merge into the branch you are on

Git always merges into the current branch. That direction matters. If main is current and you want to bring in simplify-tiers, the ordinary command has this shape:

git merge simplify-tiers

git merge asks Git to combine another branch with the branch you are on. simplify-tiers is the branch being brought in. The command does not move you to that branch, delete it, or contact another .

Before running a merge in your own work, use plain git branch to check the star beside the current branch and git status to check that your working tree is settled. This chapter’s prepared repository is already partway through a merge, so leave its state alone until lesson 3. For now, the goal is to understand what Git is trying to do.

The simplest shape is a fast-forward

Suppose main has not changed since simplify-tiers began. The other branch is simply farther along the same path:

main ------------> B
                    \
simplify-tiers -----> C -> D

There is no separate work to reconcile. Git can move the name main forward from B to D. This is called a fast-forward merge.

main ---------------------> D
simplify-tiers -----------> D

No new is needed. The existing commits already tell the whole story, and both branch names now point to the same newest commit.

That can feel surprising because the command is called merge even though Git only moved a branch pointer. The result still combines the work: the current branch now reaches the commits from the other branch.

You are on main and run git merge simplify-tiers. Which branch receives the combined result?

When can Git complete a merge by fast-forwarding?

The next lesson covers the other shape: both branches moved, so the combined history needs a commit with two parents.