Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Branching Basics  ›  Capstone Project

Merging Basics

Merging Basics 12 mingit merge brings another branch into the current one, either by fast-forwarding the label or by creating a merge commit, and stops for you to resolve conflicting lines.

Merging Basics

The vegan pancakes were a success. Now that recipe needs to come home to main, where everyone reads it. Merging is how the work from one branch joins another, and knowing which kind of merge Git will do removes most of the mystery.

The rule

Stand on the branch that receives, then merge in the branch that gives.

Merging always brings another branch into the branch you are on:

git switch main
git merge vegan-pancakes

Git then does one of two things.

Fast-forward. If main has not moved since the branch was created, main is simply behind. Git slides the main label forward to the branch's tip. No new commit is made.

Updating 9b2d7e4..c41a0f2
Fast-forward
 pancakes.md | 6 +++---

Merge commit. If both branches gained new commits, their histories have split. Git combines the two tips, using the commit where they split as a reference, and records a new merge commit with two parents. Your editor may open with a ready-made message such as Merge branch 'vegan-pancakes'; save and close it to finish.

Conflicts. When both branches changed the same lines of the same file, Git cannot choose for you. It stops and marks the file:

<<<<<<< HEAD
2 cups milk
=======
2 cups oat milk
>>>>>>> vegan-pancakes

The part above ======= is your current branch; the part below is the incoming one. Edit the file to the version you want, delete the three marker lines, then git add pancakes.md and git commit to finish the merge. If you would rather back out, git merge --abort returns everything to how it was before the merge. Resolving tricky conflicts and choosing between merge strategies are topics for more advanced Git work.

After a successful merge, the branch has done its job: git branch -d vegan-pancakes.

The shortcut

Before merging, ask "Has the receiving branch moved since we split?" Check with git log --oneline --graph --all. One straight line means a fast-forward; a fork means a merge commit, and possibly conflicts where the same lines changed.

Bringing vegan pancakes homeStep 1 of 4
  1. Receive on main

    git switch main. The receiving branch is the one you stand on.

The trap is merging the wrong way round. git merge main while standing on vegan-pancakes brings main's changes into the experiment, and main itself does not change at all.

Drills

Answer fast — trust the rule.

quick drill

Asha wants the vegan-pancakes work inside main. What should she run?

quick drill

main has no new commits since vegan-pancakes was created. What kind of merge happens?

quick drill

How many parents does a merge commit have?

quick drill

During a conflict, what does the text between <<<<<<< HEAD and ======= show?

quick drill

Mid-conflict, Asha decides she is not ready to merge. Which command puts things back?

Remember

  • Stand on the branch that receives, then merge in the branch that gives.
  • A straight line means fast-forward; a fork means a merge commit.
  • Conflicts: edit to the version you want, remove the markers, git add, git commit, or git merge --abort.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainTwo kinds of merge

Explain how Asha merges vegan-pancakes into main, when the merge will be a fast-forward and when it will create a merge commit, and what she must do if Git reports a conflict in pancakes.md.