Branches · lesson 3 of 7
Commands and ideas in this lesson
Agit mergebring a branch's work into this one
BFast-forwardjust moving the label
CMerge commita commit with two parents
DMerge basewhere the two branches split

Merging

Last updated

Merging brings a branch's work into the branch you're on. Sometimes Git only has to move a label (a fast-forward); sometimes it makes a commit with two parents (a merge commit). In this lesson you'll predict which, then do both.

By the end of this lesson you can

  • Merge a branch into the one you're on with git merge
  • Tell a fast-forward from a merge commit, and predict which you'll get
  • Find the merge base of two branches
  • Say what --no-ff does and why some teams use it

Fast-forward: just moving the label

git merge desserts means: bring desserts' work into the branch I'm on.

If your branch hasn't moved since desserts split off, desserts' commits simply come after yours. Nothing needs combining: Git moves your branch's label forward to desserts' commit and says Fast-forward. No new commit is made.

$ git log --oneline --graph --all -4
* 647fe2c (desserts) Move leche flan into desserts
* cfe7887 Add turon recipe
* 67e435d (HEAD -> main) Cover the leche flan with foil
* ba04ca2 Add lumpiang shanghai recipe
  1. 647fe2c desserts Move leche flan into desserts
  2. cfe7887 Add turon recipe
  3. 67e435d HEAD →main Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe

and 2 older commits

1 of 2 main hasn’t moved since desserts was made. desserts is two commits ahead, on the same line.

Three-way merges and merge commits

If both branches have new commits since they split, there's no line to move along. Git has to combine the two. It looks at three commits:

  • the merge base: the last commit both branches share, where they split;
  • the tip of your branch;
  • the tip of theirs.

For each file, Git compares both tips with the base. A change on only one side is taken. Changes on both sides to different lines are both taken. (Changes on both sides to the same lines are a conflict: lesson 3.4.)

Then Git records the result as a merge commit: a commit with two parents, one from each branch. In the graph, it's where two lines join.

$ git add halo-halo.md
$ git commit -m "Add halo-halo recipe"
[main fdf8b98] Add halo-halo recipe
 1 file changed, 1 insertion(+)
 create mode 100644 halo-halo.md
$ git merge-base main desserts
67e435d502cce20f1ebb777aa652ce90cc9e4258
  1. fdf8b98 HEAD →main Add halo-halo recipe
  2. 647fe2c desserts Move leche flan into desserts
  3. cfe7887 Add turon recipe
  4. 67e435d Cover the leche flan with foil
  5. ba04ca2 Add lumpiang shanghai recipe
  6. 98a5edc Add a script that builds the index page
  7. d078487 Make the sinigang steps clearer
  8. 145dc1e Add pancit bihon recipe
  9. 52d1f4d Say how much salt goes in the adobo
  10. 1c17fad Add a README

and 3 older commits

1 of 3 This time main has a commit of its own, the halo-halo recipe, made after desserts split off. The two branches have diverged.

git merge-base main desserts prints the merge base's hash.

Predict the graph

Predict the graph

Now:

  1. 647fe2c desserts Move leche flan into desserts
  2. cfe7887 Add turon recipe
  3. 67e435d HEAD →main Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer

and 6 older commits

You run git merge desserts. Which graph do you get?

Choose a graph
Predict the graph

Now:

  1. fdf8b98 HEAD →main Add halo-halo recipe
  2. 647fe2c desserts Move leche flan into desserts
  3. cfe7887 Add turon recipe
  4. 67e435d Cover the leche flan with foil
  5. ba04ca2 Add lumpiang shanghai recipe
  6. 98a5edc Add a script that builds the index page

and 7 older commits

You run git merge desserts. Which graph do you get?

Choose a graph

Merge both ways

Git sandbox
  1. On main, merge desserts. It should fast-forward
  2. Make a branch pancakes, commit a file on it, switch back to main and commit a different file there
  3. Merge pancakes into main, making a merge commit

Working folder

no changes

Staging area

nothing staged

Repository

10 commits on main

You are in ~/kusina on main. A branch called desserts has two commits that main doesn’t.

Commit graph

  1. 647fe2c desserts Move leche flan into desserts
  2. cfe7887 Add turon recipe
  3. 67e435d HEAD →main Cover the leche flan with foil
  4. ba04ca2 Add lumpiang shanghai recipe
  5. 98a5edc Add a script that builds the index page
  6. d078487 Make the sinigang steps clearer
  7. 145dc1e Add pancit bihon recipe
  8. 52d1f4d Say how much salt goes in the adobo
  9. 1c17fad Add a README
  10. 9c53888 Add leche flan recipe
  11. 1f359a2 Add sinigang recipe
  12. 601c654 Add adobo recipe

The merge commit's message, Merge branch 'pancakes', is made by Git. A real terminal opens your editor so you can add to it.

--no-ff

git merge --no-ff desserts makes a merge commit even when a fast-forward was possible. Some teams prefer that, because the graph then shows exactly which commits arrived together as one piece of work, and the whole piece can be undone by reverting one merge commit. Others prefer fast-forwards, for a straighter history. It's a team choice; lesson 6.5 compares the two views.

Spot the trap: expecting one kind and getting the other

Spot the trapAna

Ana’s team agreed every feature branch should show up in the graph as its own loop. She merges the desserts branch with plain git merge desserts. Git says Fast-forward.

What happens if…

Check what you learned

When does git merge fast-forward?
How many parents does a merge commit have?
What is the merge base?

Sources

Every command on this page behaves as these pages describe, and the sandbox prints what Git 2.55 prints. Hosting features (pull requests, protected branches) come from the host’s own documentation.

All sources for Simple Git