git merge: bring the work home
Take the commits from one universe into another.
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.
git switch main
git merge featureMerge = 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.
Updating a1b2c3d..f4e5d6
Fast-forward
feature.txt | 1 +
1 file changed03When 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.
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.
01.Merge feature into main.
hint: Make sure you are on main, then: git merge feature
02.Check the history now includes the feature commit.
hint: Try: git log --oneline