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
Press Play or use → to step through. Watch the graph as each command runs.
Read this demo as text
$ git log --oneline --all
loginhas two new commits;mainhasn't moved sinceloginbranched off.mainis simply behind.$ git merge login
Fast-forward! Git just slid
mainforward tologin's commit. No merge commit was needed — there was nothing to combine.$ git branch -d login
The work is in
mainnow, so theloginpointer is redundant. Deleting merged branches keeps the list tidy.$ git switch -c cart $ echo "cart logic" > cart.js $ git add cart.js $ git commit -m "Add cart"
New feature branch
cartwith one commit.$ 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.$ 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.
$ git log --oneline
History now contains both
Add cartandAdd footer, tied together by the merge commit.$ git branch -d cart
Merged, so safe to delete. The commits live on inside
main.
- Your branch had no new commits
- Pointer just slides forward
- No new commit, straight history
- 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 branchgit merge --no-ff <branch>Always create a merge commitgit log --oneline --allSee how branches relate before merginggit branch -d <branch>Clean up a branch after merging3 Check your understanding
You are on main and run git merge feature. What happens with this graph?
And with this graph, on main, git merge feature?
What is the "merge base"?
You want to bring feature into main. Which is right?
4 Practice for real
Land the feature
main and feature have diverged. Merge feature into main (creating a merge commit), then delete the feature branch.