Lesson 2 of 7 in Branching & Merging
Fast-forward merges
Story: Juno's timeline ran ahead while the main one stood still. Time to bring the main one up to speed.
A branch lets you work on something without touching main. When the work is done, you merge it: git merge <branch> brings that branch's commits into the branch you're on.
Merge into the branch you're on
git merge always changes the branch you're on, never the one you name. To bring contact into main, switch to main first:
git switch main
git merge contact
The other way round, git merge main while you're on contact, would bring main into contact instead, and leave main where it was.
Fast-forward
Say main hasn't moved since contact split off from it. Then contact is simply ahead: its history already has every commit on main, plus some more. There's nothing to combine, so git moves the main label forward to contact's commit. That's a fast-forward. It makes no new commit, and the history stays a straight line.
$ git merge contact
Updating a4b8d97..0e81585
Fast-forward
contact.html | 1 +
index.html | 1 +
2 files changed, 2 insertions(+)
create mode 100644 contact.html
Updating names the commit main was on and the one it moved to. Below that, git sums up what changed in your files.
When there's nothing to bring in, because every commit on the other branch is already on yours, git merge says Already up to date. and changes nothing.
A merge brings every commit
A merge brings in every commit the other branch has and yours doesn't, not only its newest one. If you started colours from heading, then colours has heading's commit too, and merging colours brings in both. The Live Graph shows it: heading's commit is in colours' line of history.
Deleting a merged branch
Once a branch is merged, its commits are on main too, so git branch -d deletes the branch without complaint. Only the label goes: the commits stay where they are. git branch -d checks the branch you're on: it deletes a branch whose commits are all in your branch's history, and refuses otherwise.
Keeping a straight line
A fast-forward is only possible while the branch you're on hasn't moved. If you commit on docs and then merge main into it, both have moved on, and git has to join them with a merge commit instead. That's the next Lesson. To bring a branch up to date and keep its history a straight line, merge first, then commit.
| Command | What it does |
|---|---|
git merge <branch> |
Brings <branch>'s commits into the branch you're on. If your branch hasn't moved since <branch> split off, it fast-forwards: your branch's label moves forward, and no new commit is made. |
New commands
git merge
Explainer: A fast-forward moves the label
Graph as text
project: your repo, you are here
- HEAD → main at 0e81585 "Link to the contact page"
Branches and tags
- main at 0e81585 "Link to the contact page"
Commits, newest first
- 0e81585 "Link to the contact page"; parent 5b2a470; refs: HEAD → main
- 5b2a470 "Add a contact page"; parent a4b8d97
- a4b8d97 "Style the heading"; parent ab89288
- ab89288 "Add the home page"; parent 37d7cf7
- 37d7cf7 "Add a README"; no parent
Challenge 1 of 3
Story: The contact page is finished, Agent. Fold it into the main timeline.
You're on contact-page, which has a finished contact page in one commit. Bring that commit into main, and end on main.
Unlocks after Basics.
Terminal
Unlocks after Basics.
This Lesson can be played once Basics is complete. Its text and Explainer are open to everyone.