In this video, you compared the merge you made for the holiday fixes with the merge that will finish the move, and saw how the shape of the history decides what git merge does. Here are the key points:
-
When both lines have new commits, Git makes a merge commit. Since
viteandmainsplit at the commit that planned the move, you made five commits onvitefor the move and two onmainfor the holiday fixes. When you mergedmainintovite, Git looked back to the last commit both lines share, combined the changes from both sides since then, and put the result in a new commit with two parents. -
Only the branch you’re on moves. Git moved
vite, the branch you were on, to the new merge commit, andmainstayed where it was. -
When the commit you’re on is already in the other branch’s history, Git fast-forwards. At the end of the course, you’ll switch to
mainand rungit merge vite. If you follow the arrows back from the newest commit onvite, through the merge commit’s other parent and over to the line ofmain, you reach the commitmainpoints to. -
A fast-forward only moves the name. The line of
mainhas no new commits to combine. Git only moves the namemainforward to the newest commit onvite. It doesn’t make a new commit, and both names end up on the same commit. -
The direction of the merge doesn’t decide this. If
maingot another fix first, mergingviteintomainwould make a merge commit too. -
Git’s message tells you which kind you got. A merge commit’s output says
Merge made by the 'ort' strategy., and a fast-forward’s saysFast-forward. -
After a fast-forward, the log shows no new merge commit. If you’re expecting a merge commit, that can look like the merge didn’t happen. The merge still worked, and the newest commit on
mainhas one parent. A merge commit keeps both lines visible, joined at a commit of their own. -
Two options change the default.
git merge --no-ffmakes a merge commit instead of fast-forwarding, for example to keep a feature’s commits grouped. Withgit merge --ff-only, Git merges only when it can fast-forward, and otherwise stops with an error. You won’t need either one in this course.
This is the holiday merge’s output from the last lesson, the one with a merge commit:
$ git merge main
Auto-merging README.md
Merge made by the 'ort' strategy.
README.md | 4 ++--
src/data/holidays.js | 4 ++--
2 files changed, 4 insertions(+), 4 deletions(-)
So when the other branch has commits you don’t have, look at the shape of the history before you merge. If the commit you’re on is already in the other branch’s history, Git fast-forwards and only moves the name. If both lines have new commits since they split, Git makes a merge commit with two parents.