Merging branches

Topic 3 of 4 in Branching and merging7 min readBeginnerFree course

Overview

Branches split work apart; merging brings it back together. You switch to the branch that should receive the work, then merge the other branch into it:

Terminal
git switch main
git merge feature/contact

Git then does one of two things, depending on the shape of the history.

Fast-forward and three-way merges. Fast-forward: main has not moved since feature branched off, so after a1 and 7d come e4 and f5 on feature, and main just slides forward to f5 — no merge commit. Three-way: both branches have new commits — b2 on main and e4 on the feature branch, both after 7d — so Git creates a merge commit M whose two parents are b2 and e4, and main points to M. Commands: git switch main; git merge feature prints Updating 7d02b4e..f5a9c31 and Fast-forward, or Merge made by the ‘ort’ strategy; then git branch -d feature removes the branch label, which is no longer needed.

Fast-forward merges

If main hasn’t changed since the feature branch was created, the feature’s commits simply continue from main’s tip. There’s nothing to combine — Git just moves main forward to the feature’s tip:

Text
Updating 7d02b4e..f5a9c31
Fast-forward
 about.html | 1 +
 1 file changed, 1 insertion(+)

No new commit is created, and history stays a straight line.

Three-way merges

If both branches have new commits, Git needs to combine two different sets of changes. It looks at three snapshots:

  1. the tip of your current branch,
  2. the tip of the branch being merged,
  3. their merge base — the most recent commit they have in common.

From the merge base, Git works out what each side changed and applies both sets of changes. If they touched different files, or different parts of the same file, the result is automatic. Git records it in a new merge commit:

Text
Merge made by the 'ort' strategy.
 contact.html | 1 +
 1 file changed, 1 insertion(+)

(ort is Git’s default merge algorithm.) Git opens your editor for the merge commit’s message, pre-filled with Merge branch 'feature/contact'; --no-edit accepts it as is.

A merge commit is special in one way: it has two parents — the previous tip of main and the tip of the merged branch:

Terminal
git log -1 --format="%p"
# 3c4d5e6 8a9b0c1

That’s how Git remembers that the two lines of history were joined, and why later merges of the same branches know what’s already been combined.

If both sides changed the same lines differently, Git can’t decide which to keep — that’s a merge conflict, covered in the next lesson.

Choosing the merge style

Terminal
git merge --no-ff feature/contact   # always create a merge commit, even if a fast-forward is possible
git merge --ff-only feature/about   # only fast-forward; refuse if a real merge would be needed
  • --no-ff keeps a visible record that a group of commits arrived together as one feature, which some teams like in their history.
  • --ff-only guarantees a linear history: if main has moved on, the merge fails, and you update the feature branch first (by rebasing it — next chapter).

On GitHub, the merge button on a pull request makes these choices for you — “Create a merge commit” uses --no-ff, and there are squash and rebase options too (Pull requests).

Merging main into your branch

Merges work in both directions. On a long-running feature branch, you can bring in the latest main to stay up to date and catch conflicts early:

Terminal
git switch feature/checkout
git merge main

The alternative is to rebase the branch onto main, which keeps history linear. Merge or rebase? compares the two.

Undoing a merge

  • Before committing (for example during a conflict): git merge --abort puts everything back.
  • Just merged, not pushed: git reset --hard ORIG_HEAD — Git saves the pre-merge position as ORIG_HEAD.
  • Already pushed: revert the merge commit with git revert -m 1 <merge> (Amend and revert).

Cleaning up

Once a branch is merged, its commits are reachable from main, so the branch name is no longer needed:

Terminal
git branch -d feature/contact
git branch --merged main      # list other branches that are safe to delete

Seeing merges in history

Terminal
git log --oneline --graph
Text
*   9f8e7d6 (HEAD -> main) Merge branch 'feature/contact'
|\
| * 8a9b0c1 Add contact page
* | 3c4d5e6 List pages in README
|/
* f5a9c31 Add about page
* 1a2b3c4 Start site

Useful variations: git log --merges (only merge commits), git log --no-merges (skip them), and git log --first-parent main (only the commits made directly on main, plus one entry per merge — a tidy summary of what landed when).

Before merging: make sure your working directory is clean (git status) and you’re on the receiving branch (git branch --show-current). Most merge confusion starts with one of those two.

Exercise

One fast-forward, one true merge

(1) Create feature/about from main, add about.html in a commit, and merge it into main — it should fast-forward. (2) Create feature/contact and commit contact.html. Meanwhile, back on main, commit a change to README.md. Merge feature/contact into main: it must create a merge commit. (3) Check that the merge commit has two parents, delete both feature branches safely, and view the graph.

Terminal
mkdir merges && cd merges && git init -b main
echo "# Site" > README.md && git add . && git commit -qm "Start site"

# 1. A fast-forward merge

# 2. A three-way merge

# 3. Check the merge commit, tidy up, view the graph

Summary

  • git merge <branch> brings another branch’s commits into the current branch.
  • If the current branch hasn’t moved, Git fast-forwards: it just moves the pointer, no new commit.
  • If both branches have new commits, Git makes a merge commit with two parents.
  • --no-ff always creates a merge commit; --ff-only refuses unless a fast-forward is possible.
  • After merging, delete the feature branch — its commits are now reachable from main.