learn git.
0/25 done
learn git/branches/course 17 of 25

git merge: bring the work home

Take the commits from one universe into another.

8 min read·4 sections·2 tasks
the key idea

You merge while standing on the destination — git merge feature brings feature's work into where you are.

01The rule of direction

Merging answers one question: bring the work from that branch into this one. The direction matters, and it trips up everyone at first. You stand on the branch you want the work to arrive in — almost always main — and you name the branch the work comes from. git merge feature, run from main, means: make main include everything feature did.

Run it backwards — standing on feature, merging main — and you get the opposite: feature absorbs main's work. The command is symmetrical; your position decides the direction. Check the star before you merge.

[bash]· stand where it lands, name where it comes from
git switch main
git merge feature
Destination first

Merge = stand on the destination, name the source. If you remember one sentence from this course, make it that one.

02The friendly case: fast-forward

The easiest merge happens when the destination has not moved since the branch was created. main is exactly where feature left it, so there is nothing to combine — git simply slides main's label forward to feature's newest commit. Git tells you it did this by printing Fast-forward, along with the files that arrived.

It is called fast-forward because no new commit is created — the label just moves forward along the existing chain. The histories reconnect without ceremony, and the log looks like the branch work happened in a straight line.

[bash]· what an easy merge looks like
Updating a1b2c3d..f4e5d6
Fast-forward
 feature.txt | 1 +
 1 file changed

03When there is nothing to do

Merging a branch whose work is already present gets you Already up to date. — not an error, just git confirming the destination already contains everything. You will see this constantly when you merge twice by accident or merge a branch you already merged.

04And when both sides moved?

When main and the branch both collected new commits, git can still usually merge automatically — it combines the two lines, file by file, taking both sides' changes. That combined photograph is called a merge commit, and git announces it with Merge made by the 'ort' strategy.

But if both sides changed the same lines of the same file, no machine can know which wording you meant. Git stops and asks for a human decision — a conflict. That moment is important enough to get its own course, which is exactly next.

What the merge commit is

A normal commit remembers one parent — the photograph before it. A merge commit remembers two. That is the whole trick: one photograph with two parents, holding the combined state of both lines.

>>

Practice

The feature work is done on the branch. Bring it home to main.

[practice]· a real terminal, safely simulated
practice terminal — everything here is simulated, nothing can break
type 'help' to see the commands this terminal understands
~/my-project (main) $
Your tasks0/2 done
  1. 01.Merge feature into main.

    hint: Make sure you are on main, then: git merge feature

  2. 02.Check the history now includes the feature commit.

    hint: Try: git log --oneline