Git Merge: Combine Branches and Understand Fast-Forwards

Boot.dev Blog » DevOps » Git Merge: Combine Branches and Understand Fast-Forwards
ThePrimeagen
ThePrimeagen Ex-Netflix engineer, NeoVim ricer, and Git rebaser

Last published March 31, 2026

Table of Contents

“What’s the point of having multiple branches?” you might ask. They’re most often used to safely make changes without affecting your (or your team’s) primary branch. Once you’re happy with your changes, you’ll want to merge them back into main so that they make their way into the final product.

All the content from our Boot.dev courses are available for free here on the blog. This one is the “Merge” chapter of Learn Git. If you want to try the far more immersive version of the course, do check it out!

How Does a Merge Work?

Let’s say you have two branches, each with their own unique commits:

A - B - C    main
   \
    D - E    other_branch

If you merge other_branch into main, Git combines both branches by creating a new commit that has both histories as parents. In this diagram, F is a merge commit that has C and E as parents. F brings all the changes from D and E back into the main branch.

A - B - C - F    main
   \     /
    D - E        other_branch

To do this, you switch to the branch you want to merge into, then run git merge:

git switch main
git merge other_branch

What Happens During a Merge Commit?

The merge will:

  1. Find the “merge base” commit, or “best common ancestor” of the two branches.
  2. Compare what changed on each branch since that merge base.
  3. Combine those changes into a single merged snapshot.
  4. Record that merged snapshot as a new commit with two parents.

That new commit is special because it has two parents. A normal commit has one parent. A merge commit has two.

How Do You Read Merge History in git log?

After a merge, git log --oneline --graph --parents will show something like:

*   89629a9 d234104 b8dfd64 (HEAD -> main) F: Merge branch 'add_classics'
|\
| * b8dfd64 fba0999 (add_classics) D: add classics
* | d234104 fba0999 E: update contents
|/
* fba0999 1381199 C: add quotes
* 1381199 a21228f B: add titles.md
* a21228f A: add contents.md

Each asterisk * represents a commit. There are multiple hashes on some lines because the --parents flag logs the parent hash(es) as well.

  • The first line has three hashes: 89629a9 is the merge commit, and the other two (d234104, b8dfd64) are its parents.
  • The next section is a visual representation of the branch structure before the merge. Notice that both branches share a common parent.
  • The last line is the initial commit and therefore has no parent.

What Is a Fast-Forward Merge?

The simplest type of merge is a fast-forward merge. Say we start with this:

      C     delete_vscode
     /
A - B       main

Because delete_vscode has all the commits that main has (and main has no new commits of its own), Git can just move main’s pointer forward:

            delete_vscode
A - B - C   main

No merge commit is created. Git does this automatically when it can.

This is a common workflow when working with Git on a team:

  1. Create a branch for a new change
  2. Make the change
  3. Merge the branch back into main
  4. Remove the branch
  5. Repeat

You delete a merged branch with:

git branch -d branch_name

[!tip] If the merge can be a fast-forward, Git will do it automatically. If both branches have diverged (both have unique commits), Git creates a merge commit instead. You don’t have to choose — Git figures it out.

If you want the no-extra-merge-commit flavor, read Git Rebase. I prefer rebasing my own feature branches because a linear history is easier to read and reason about. Rebase is based.

Frequently Asked Questions

What is a merge commit?

A merge commit is a special commit that has two parents. It is the result of combining two branches that have diverged from a common ancestor.


What is a fast-forward merge?

A fast-forward merge happens when the base branch has no new commits since the feature branch was created. Git just moves the base branch pointer forward to the tip of the feature branch. No merge commit is created.


How do I merge a branch into main?

Switch to main with git switch main, then run git merge your_branch_name. If main has no new commits, Git will fast-forward. Otherwise it creates a merge commit.


What is the merge base?

The merge base is the best common ancestor of the two branches being merged. Git uses it as the starting point to figure out what changed on each branch.


How do I read merge history in git log?

Use git log --oneline --graph --parents. The --graph flag draws an ASCII branch structure and --parents shows the parent hashes for each commit, so merge commits will display two parent hashes.