Gitastic
Path › Part 2: Branching › Lesson 5 of 16
05

Merging

Bringing work back together: fast-forward or merge commit.

1 Learn the idea

git merge <branch> brings the commits of another branch into the branch you are on. Depending on what the history looks like, git does one of two very different things.

Case 1: fast-forward

If your branch has no new commits since the other branch split off, git simply slides your branch pointer forward. No new commit is created — history stays a straight line.

Case 2: three-way merge

If both branches have new commits, git finds their merge base (the last common ancestor) and compares three snapshots: the base, yours, and theirs. Changes from both sides are combined into a new merge commit that has two parents.

2 Watch it happen

Fast-forward, then a real merge

Press Play or use → to step through. Watch the graph as each command runs.

f31cf7f Initial shopf31cf7fInitial shop6844884 Add login form6844884Add login formb4c6d53 Validate passwordb4c6d53Validate passw…HEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)mainBranch "login" — a movable pointerlogin
terminal · simulated, no real repository is touched
Read this demo as text
  1. $ git log --oneline --all

    login has two new commits; main hasn't moved since login branched off. main is simply behind.

  2. $ git merge login

    Fast-forward! Git just slid main forward to login's commit. No merge commit was needed — there was nothing to combine.

  3. $ git branch -d login

    The work is in main now, so the login pointer is redundant. Deleting merged branches keeps the list tidy.

  4. $ git switch -c cart
    $ echo "cart logic" > cart.js
    $ git add cart.js
    $ git commit -m "Add cart"

    New feature branch cart with one commit.

  5. $ git switch main
    $ echo "© 2026" > footer.html
    $ git add footer.html
    $ git commit -m "Add footer"

    Meanwhile, someone commits directly on main. Now the two branches have diverged — each has a commit the other lacks.

  6. $ git merge cart

    No fast-forward is possible, so git finds the merge base, combines both sides, and creates a merge commit with two parents. Look for the double ring.

  7. $ git log --oneline

    History now contains both Add cart and Add footer, tied together by the merge commit.

  8. $ git branch -d cart

    Merged, so safe to delete. The commits live on inside main.

Fast-forward
  • Your branch had no new commits
  • Pointer just slides forward
  • No new commit, straight history
Merge commit
  • Both branches have new commits
  • Three-way combination via the merge base
  • New commit with two parents
git merge <branch>Bring <branch> into the current branch
git merge --no-ff <branch>Always create a merge commit
git log --oneline --allSee how branches relate before merging
git branch -d <branch>Clean up a branch after merging

3 Check your understanding

Question 1

You are on main and run git merge feature. What happens with this graph?

b01499b Initialb01499bInitial2c08082 Feature A2c08082Feature A5317130 Feature B5317130Feature BHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)mainBranch "feature" — a movable pointerfeature
Question 2

And with this graph, on main, git merge feature?

b01499b Initialb01499bInitial2c08082 Feature A2c08082Feature A40ffa4d Hotfix40ffa4dHotfixBranch "feature" — a movable pointerfeatureHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
Question 3

What is the "merge base"?

Question 4

You want to bring feature into main. Which is right?

4 Practice for real

Your mission

Land the feature

main and feature have diverged. Merge feature into main (creating a merge commit), then delete the feature branch.

220addd Initial commit click to insert the hash into the terminal220adddInitial commitfc709e4 Build feature click to insert the hash into the terminalfc709e4Build featured40d112 Polish feature click to insert the hash into the terminald40d112Polish featurec14ecf7 Hotfix on main click to insert the hash into the terminalc14ecf7Hotfix on mainBranch "feature" — a movable pointerfeatureHEAD = where you are. It points at the current branch.HEAD →Branch "main" — a movable pointer (current branch: moves when you commit)main
terminal · simulated, no real repository is touched
Type commands below. `help` lists everything. Tab completes, ↑ recalls. Click a commit to paste its hash.
~/project (main) $