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
647fe2cdesserts Move leche flan into dessertscfe7887Add turon recipe67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe
and 2 older commits
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
fdf8b98HEAD →main Add halo-halo recipe647fe2cdesserts Move leche flan into dessertscfe7887Add turon recipe67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README
and 3 older commits
git merge-base main desserts prints the merge base's hash.
Predict the graph
Now:
647fe2cdesserts Move leche flan into dessertscfe7887Add turon recipe67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer
and 6 older commits
You run git merge desserts. Which graph do you get?
Now:
fdf8b98HEAD →main Add halo-halo recipe647fe2cdesserts Move leche flan into dessertscfe7887Add turon recipe67e435dCover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index page
and 7 older commits
You run git merge desserts. Which graph do you get?
Merge both ways
- On main, merge
desserts. It should fast-forward - Make a branch
pancakes, commit a file on it, switch back to main and commit a different file there - Merge
pancakesinto 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
647fe2cdesserts Move leche flan into dessertscfe7887Add turon recipe67e435dHEAD →main Cover the leche flan with foilba04ca2Add lumpiang shanghai recipe98a5edcAdd a script that builds the index paged078487Make the sinigang steps clearer145dc1eAdd pancit bihon recipe52d1f4dSay how much salt goes in the adobo1c17fadAdd a README9c53888Add leche flan recipe1f359a2Add sinigang recipe601c654Add 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
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
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.
- Git documentation: git-merge: fast-forward merges, true merges and --no-ff
- Git documentation: git-merge-base: the best common ancestor
- Pro Git: Basic Merging: fast-forward and three-way merges