Merging branches
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:
git switch main
git merge feature/contactGit then does one of two things, depending on the shape of the history.
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:
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:
- the tip of your current branch,
- the tip of the branch being merged,
- 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:
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:
git log -1 --format="%p"
# 3c4d5e6 8a9b0c1That’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
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-ffkeeps a visible record that a group of commits arrived together as one feature, which some teams like in their history.--ff-onlyguarantees a linear history: ifmainhas 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:
git switch feature/checkout
git merge mainThe 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 --abortputs everything back. - Just merged, not pushed:
git reset --hard ORIG_HEAD— Git saves the pre-merge position asORIG_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:
git branch -d feature/contact
git branch --merged main # list other branches that are safe to deleteSeeing merges in history
git log --oneline --graph* 9f8e7d6 (HEAD -> main) Merge branch 'feature/contact'
|\
| * 8a9b0c1 Add contact page
* | 3c4d5e6 List pages in README
|/
* f5a9c31 Add about page
* 1a2b3c4 Start siteUseful 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.
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.
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 graphmkdir merges && cd merges && git init -b main
echo "# Site" > README.md && git add . && git commit -qm "Start site"
# 1. A fast-forward merge
git switch -c feature/about
echo "<h1>About</h1>" > about.html && git add . && git commit -qm "Add about page"
git switch main
git merge feature/about # "Fast-forward"
# 2. A three-way merge
git switch -c feature/contact
echo "<h1>Contact</h1>" > contact.html && git add . && git commit -qm "Add contact page"
git switch main
echo "Pages: home, about, contact" >> README.md && git commit -qam "List pages in README"
git merge --no-edit feature/contact # "Merge made by the 'ort' strategy."
# 3. Check the merge commit, tidy up, view the graph
git log -1 --format="%p" # two parent IDs
git branch -d feature/about feature/contact
git log --oneline --graphSummary
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-ffalways creates a merge commit;--ff-onlyrefuses unless a fast-forward is possible.- After merging, delete the feature branch — its commits are now reachable from main.