Find a course

Module 3: Branching and MergingLesson 7 of 1210 min

Merging Branches

Combine branches with git merge, tell a fast-forward from a merge commit, and know when teams choose merging or rebasing.

Step 1 of 12Learn

Bringing the work home

Two weeks later, Lena's new layout is finished and tested on the new-layout branch. Time to make it live. She switches to the branch that should receive the changes and merges the other one into it:

git switch main

git merge new-layout

That's the whole command. The direction matters and is a common source of confusion: you stand on the target branch and name the source branch. Running git merge main while on new-layout would do the reverse, bringing main's latest changes into the redesign branch. That's also useful, as you'll see, but it doesn't make anything live.

Git then combines the histories in one of two ways, depending on whether main moved while Lena was working on the layout. If nobody committed to main in the meantime, the merge is trivial. If both branches have new commits, Git has to combine two sets of changes. Understanding which case you're in explains the messages Git prints and the shape of your history.

Step 2 of 12Compare

Fast-forward vs. three-way merge

AspectFast-forwardThree-way merge
WhenMain has no new commits since the branch startedBoth branches have new commits
What Git doesMoves the main pointer forwardCombines both lines using their common ancestor
New commit?NoYes, a merge commit with two parents
History shapeStraight lineA visible fork and join

Step 3 of 12Learn

Fast-forward merges

Suppose nobody touched main while Lena worked. The history looks like this:

A --- B --- C              main
             \
              D --- E --- F   new-layout

main is at C, and new-layout simply continues from C. There's nothing to combine: main just needs to move forward to F. Git does exactly that and reports Fast-forward:

A --- B --- C --- D --- E --- F   main, new-layout

No new commit is created. The history is a straight line, as if the work had been done on main all along.

Some teams prefer to record that a feature branch existed, even when a fast-forward is possible. git merge --no-ff new-layout forces Git to create a merge commit anyway, so the history shows the group of commits that made up the feature.

After a successful merge, the feature branch has done its job. Delete it with git branch -d new-layout. Its commits aren't lost: they're now part of main's history. Only the label goes away.

Step 4 of 12Practice · Diagram

Which kind of merge will you get?

Both main and the feature have new commits. Which result do you get?

Tap the shape that answers the question, then press Check.

Which kind of merge will you get?Two-by-two matrix: Fast-forward, Merge commit, Already up to date, Feature is behind.Fast-forwardMerge commitAlready up to dateFeature is behindNew commits on main since branchingNoneSomeNew commits on the featureNoneSome
Fast-forward

main just moves ahead to the feature's last commit. No new commit.

Merge commit

Git combines both lines and records a commit with two parents.

Already up to date

Nothing to bring in.

Feature is behind

The feature has nothing new, so merging it does nothing. Usually you merge main into it instead.

Step 5 of 12Learn

Three-way merges and merge commits

Now suppose her brother's typo fix, commit G, landed on main while Lena was working:

A --- B --- C --- G          main
             \
              D --- E --- F   new-layout

The branches have diverged: each has commits the other lacks. main can't simply slide forward to F, because that would drop G. So Git performs a three-way merge. It looks at three snapshots: the common ancestor (C), the tip of main (G) and the tip of new-layout (F). For each file, it works out what each side changed relative to C and combines both sets of changes.

The result is a new merge commit, M, with two parents:

A --- B --- C --- G ------- M   main
             \             /
              D --- E --- F     new-layout

Git opens your editor with a default message such as Merge branch 'new-layout', which is usually fine to accept.

As long as the two branches changed different files, or different parts of the same file, Git combines them automatically. Only when both changed the same lines does it stop and ask you to decide. That's a merge conflict, the topic of the next lesson.

Step 6 of 12Practice · Sort into groups

Would each merge into main be a fast-forward or need a merge commit (with default settings)?

Tap an item, then tap its group (or drag it there).

Step 7 of 12Practice · Put in order

Put the steps for merging a finished branch called add-search into main in order.

Drag the items, or use the arrow buttons, to put them in order.

  1. Check the result and run the tests
  2. git merge add-search
  3. Commit any remaining work on add-search
  4. git switch main
  5. git branch -d add-search

Step 8 of 12Learn

Merge or rebase?

There's a second way to integrate branches: rebase. Instead of joining two lines with a merge commit, git rebase main (run on the feature branch) takes the feature's commits and replays them one by one on top of the latest main, as if the branch had been started from there.

A --- B --- C --- G                main
                   \
                    D' --- E' --- F'  new-layout

The result is a straight-line history, and the feature can then be fast-forwarded into main. Note the primes: D′, E′ and F′ are new commits with new hashes. Rebase rewrites history.

That leads to the rule from the undo lesson: don't rebase commits that others have already pulled. Rebasing your own local, unshared branch to tidy it up before review is fine and common. Rebasing a shared branch forces everyone else to untangle duplicated commits.

Which should a beginner use? Merging is simpler and never rewrites anything, so start there. Many teams decide at the repository level, often through the merge button options on GitHub, which you'll see in the pull request lesson. Follow your team's convention.

Step 9 of 12Compare

Merge vs. rebase

AspectMergeRebase
History shapeShows branches joining, with merge commitsStraight line, no merge commits
Rewrites existing commits?NoYes, replayed commits get new hashes
Safe on shared branches?YesOnly on branches nobody else uses
ConflictsResolved once, in the merge commitMay need resolving commit by commit
Good default for beginners?YesLearn after you are comfortable with merging

Step 10 of 12Practice · Fill in the blanks

Fill in the blanks

You're on main and want to bring in the work from the branch add-search. Run `git add-search`.

Step 11 of 12Practice · True or false

A rebase gives the replayed commits new hashes, even if their changes are the same.

Step 12 of 12Practice · Choose one

You run git merge feature on main and Git prints "Fast-forward". What happened?