Merging Branches Back
Merging
When a feature is done, you merge it back into main so its commits become part of the main line of history. The rule that trips people up: you always merge into the branch you're currently standing on - so to bring feature-login into main, you first switch to main, then merge feature-login into it (not the other way around).
$ git switch main
$ git merge feature-login
Updating 9f8e7d6..a1b2c3d
Fast-forward
login.js | 20 ++++++++++
There are two different things that can happen, depending on whether main moved while you were working:
| Type | When it happens | What Git does |
|---|---|---|
| Fast-forward | main hasn't gained any new commits since you branched off it | main's pointer just slides forward to feature-login's tip - no new commit is created, history stays a straight line |
| Three-way merge | Both main and feature-login gained new commits since you branched | Git creates a brand-new merge commit with two parents, joining both histories together |
Fast-forward: Three-way merge:
A─B (main) A─B──────M (main) ← M has two parents
\ \ /
C─D (feature) C─D (feature)
after merge: A─B─C─D (main)
Tip: The typical everyday flow:git switch main→git pull(grab any teammates' commits you don't have yet) →git merge feature. Always merge into the target branch while standing on it - never the reverse.
After merging, the feature branch has done its job - clean it up with git branch -d feature-login. (Git will refuse to delete it with -d if it has unmerged work, as a safety check.)
Warning: It's easy to accidentally merge in the wrong direction - standing onfeature-loginand runninggit merge mainmerges main's changes into your feature branch instead. That's sometimes intentional (pulling in the latest main before finishing your feature), but it is not the same as finishing the feature - double-check which branchgit statussays you're on ("On branch …") before merging.