Learn Merging in Git

A merge joins the work of one branch (a line of work) into another. Move to the target branch first (git switch main or git checkout main), then run git merge…

The new footer (the bottom part of the page) on the branch feature-footer is done, and you've already tried it in the browser. But main, the version everyone uses, still shows the old footer. Your work hasn't moved there yet.

A merge moves it: a merge joins the work of one branch into another, usually from a feature branch into main. Think of a book manuscript. You write a new chapter in a separate draft while the main manuscript sits on the editor's desk. If nobody has touched the manuscript since you started, the editor just uses your draft as the newest manuscript. If someone else has changed the manuscript too, the editor has to combine both into a new version. In Git, the main manuscript is main, your draft is the feature branch, and Git is the editor that does those two things.

Move to the target branch, then merge

A merge always brings the branch you name into the branch you're on. So first move to the target branch, the one that receives the changes. After that, name the source branch:

git switch main          # 1. move to the target branch (or: git checkout main)
git merge feature-footer   # 2. bring feature-footer into main

An example from the start to the merge

The project folder holds index.html and footer.html, and footer.html holds <footer>Copyright 2024</footer>. Here are all the commands, from git init to the merge:

git init
git add .
git commit -m "initial"
git switch -c feature-footer                            # or: git checkout -b feature-footer
echo "<footer>Copyright 2025</footer>" > footer.html  # change the footer
git add .
git commit -m "add footer"
git switch main                                       # back to the target branch
git merge feature-footer

Here is the answer git merge gives (the hashes on your computer will differ):

$ git merge feature-footer
Updating 3f2a1c9..8b7d4e0
Fast-forward
 footer.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Updating 3f2a1c9..8b7d4e0 means main is moved from commit 3f2a1c9 to commit 8b7d4e0. The next section explains Fast-forward.

Fast-forward: main just slides forward

main at feature-footer at
Before the merge initial add footer
After the merge add footer add footer

main hasn't changed since feature-footer was made, so the "add footer" commit already comes right after main. Git only has to slide main forward to that commit. This is called a fast-forward: the branch name only moves forward, and no new commit is made. main now has 2 commits, "initial" and "add footer". In the exercise, that's what is checked.

Merge commit: when both have moved forward

Say that before the merge, main got a new commit too: someone added about.html in a commit "add about page". Now the two lines of work have split, and main can't just slide forward. Git makes a merge commit: a new commit with two parents (commits before it), the tip (the newest commit) of main and the tip of feature-footer.

$ git merge feature-footer
Merge made by the 'ort' strategy.
 footer.html | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

Before that answer appears, Git opens a text editor holding the default message Merge branch 'feature-footer'. Just save and close it, or use git merge --no-edit feature-footer to merge without the text editor.

The content of the merge commit comes from a 3-way merge. Git compares three versions: the base (the commit where the two branches split), the tip of the branch you're on, and the tip of the branch being merged. For each line:

In this example about.html only changed on main and footer.html only changed on feature-footer, so the merge result holds both.

💡 Want the feature's trail to show? Use --no-ff

git merge --no-ff feature-footer (no fast-forward) always makes a merge commit, even when a fast-forward was possible. In the history, the feature's commits then show as one group, and the merge commit marks where the feature came in. Many teams do this for big features.

⚠️ Merged the wrong way, or nothing happened?

TL;DR: A merge joins the work of one branch (a line of work) into another. Move to the target branch first (git switch main or git checkout main), then run git merge branch-name: the branch you name comes into the branch you're on. If the target hasn't changed since the feature branch was made, it's a fast-forward: the main pointer just slides forward, no new commit. If both have moved forward, Git makes a merge commit with two parents (commits before it). Its content comes from a 3-way merge, which compares the base (where the two branches split), your branch and the merged branch. Both branches changing the same line means a conflict. --no-ff forces a merge commit so the feature's trail shows.

⚡ Move to the target branch first

Write:

git switch main
git merge feature-footer
Guess first: git merge joins one branch into another. In the wrong order, git switch feature-footer then git merge main: which branch changes?

feature-footer. A merge always brings the branch you name into the branch you're on, so main goes into the feature.

Meaning: Move to the target branch first, the one that should receive the changes, then name the source branch. Before you merge, check git branch: the * marks the branch you're on.

In the exercise, you must end up on main, because that's where the merge runs.

⚡ Fast-forward

Meaning: If main hasn't changed since the feature branch was made, Git just slides main forward to the feature's newest commit. This is called a fast-forward, and no new commit is made.

⚡ Merge commit

Meaning: If main has a new commit too, Git makes a merge commit: a new commit with two parents (commits before it), the newest commits of both branches. Both branches changed different lines that are not next to each other? Both are kept. Both branches changed the same line in different ways? That's a conflict, and you decide.

What you will learn