Getting Started Git Basics Merge

Merge

Understand how Git merge combines branches, when fast-forwarding applies, and how conflicts can appear.

Merge combines changes from one line of development into another, usually by bringing commits from one branch into your current branch.

When you merge, Git keeps the existing commits and histories, then records that the two lines of work now come together. That is one of the main differences between merge and rebase, which rewrites commits by replaying them elsewhere.

What merging does

Suppose main and feature/login both moved forward separately.

When you merge feature/login into main, Git tries to combine those lines of work so main contains both sets of changes.

In simple terms:

  • You stay on the branch you want to update.
  • You merge in the other branch.
  • Git combines the histories instead of replacing one with the other.
A --- B --- C  main
       \
        D --- E  feature/login

After merging feature/login into main, the history may look like this:

A --- B --- C ------- F  main
       \             /
        D --------- E  feature/login

Here, F is a merge commit. It joins the two lines of history together.

Fast-forward and merge commits

Not every merge creates a merge commit.

If your current branch has not moved since the other branch split off, Git may be able to do a fast-forward merge. In that case, Git can simply move the branch name forward to the newer commit.

If both branches have new commits, Git usually creates a merge commit instead. That commit has two parents and marks the point where the histories were joined.

Why merge is useful

Merge is commonly used to:

  • Bring a finished feature branch into main.
  • Combine a bug fix branch back into a release branch.
  • Integrate work from another branch without rewriting existing commits.

This is one reason merge is often a comfortable default for shared work. It keeps the original branch structure visible.

Merge can cause conflicts

If both lines of work changed the same code in incompatible ways, Git may stop and ask you to resolve a merge conflict.

A conflict during merge does not mean the repository is damaged. It means Git needs help deciding what the final file contents should be.

Merge vs rebase

Both merge and rebase combine work from different branches, but they do it differently.

  • Merge preserves the existing branch structure and may create a merge commit.
  • Rebase rewrites commits onto a new base and changes their identities.

Neither approach is always better. Teams choose based on their workflow, but merge is often simpler to reason about when multiple people already depend on the existing history.

Documentation

Search

move open Esc close

Browse the docs or start typing to filter results.