Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Tell the Two Kinds of Merge Apart

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 vite and main split at the commit that planned the move, you made five commits on vite for the move and two on main for the holiday fixes. When you merged main into vite, 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, and main stayed 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 main and run git merge vite. If you follow the arrows back from the newest commit on vite, through the merge commit’s other parent and over to the line of main, you reach the commit main points to.

  • A fast-forward only moves the name. The line of main has no new commits to combine. Git only moves the name main forward to the newest commit on vite. 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 main got another fix first, merging vite into main would 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 says Fast-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 main has one parent. A merge commit keeps both lines visible, joined at a commit of their own.

  • Two options change the default. git merge --no-ff makes a merge commit instead of fast-forwarding, for example to keep a feature’s commits grouped. With git 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:

Language: Shell
$ 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.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 In the last lesson, you merged main into vite to bring in the holiday fixes. Git made a new commit for that, a merge commit. At the end of the course, you’ll merge vite into main, the other way round.

00:13 But not every merge makes a merge commit. Git has two kinds of merge, and it picks one from the shape of your history.

00:22 Here’s the history from right before that merge. Each dot is a commit, pointing back to its parent. Both lines start at the commit that planned the move. Since that split, you made five commits on vite for the move, and two on main for the holiday fixes.

00:39 Each line has new commits that the other line can’t reach. When you merged, Git looked back to the last commit both lines share. It combined the changes from both sides since then.

00:51 It put the result in a new commit with two parents, one from each line. Then it moved vite, the branch you were on, to the new commit. Main stayed where it was.

01:03 A merge commit is how Git joins two lines that both moved on.

01:09 Now picture the end of the course. You’ll commit the rest of the move on vite, so vite moves further ahead. On main, you won’t commit again before that last merge.

01:20 Then you’ll switch to main and merge vite into it. Your turn: pause the video and predict whether this merge makes a merge commit.

01:34 It doesn’t. Start at vite’s newest commit and follow the arrows back. Through the merge commit’s other parent, the arrow up to main’s line, you reach the commit main points to.

01:47 So main’s commit is already in vite’s history, and main’s line has no new commits to combine.

01:54 It’s the shape that decides, not the direction. If main got another fix first, merging vite into main would make a merge commit too.

02:03 Without that extra fix, Git only moves the name main forward, to vite’s commit. That’s called a fast-forward. Git doesn’t make a new commit. Both names end up on the same dot.

02:17 Git’s message tells the two apart, too. For the holiday fixes, it said that the merge was made by its standard strategy. For the merge at the end, it’ll print Fast-forward instead.

02:30 If you’re expecting a merge commit, that can look like the merge didn’t happen. After a fast-forward, the log shows no new merge commit. The merge still worked, and main’s newest commit has one parent.

02:42 A merge commit keeps both lines visible, joined at a commit of their own. To get a merge commit anyway, add the –no-ff option, for example to keep a feature’s commits grouped.

02:55 With the –ff-only option, Git merges only when it can fast-forward, and otherwise stops with an error. You won’t need either one in this course.

03:05 So when the other branch has commits you don’t have, look at the shape 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.

03:17 If both lines have new commits since they split, Git makes a merge commit with two parents.

03:24 Before that last merge, the curbside pickup to-do needs Generic Curbside’s key. In the next lesson, you’ll learn how to keep a key like that out of your repository.

Become a Member to join the conversation.