Git and GitHub · Part 2 · Branches and merging
Merging: fast-forward vs. three-way
What merging is
Merging takes the work from one branch and combines it into another. The rule beginners mix up most: a merge goes into the current branch.
git switch main # the branch that receives the work
git merge feature # bring feature's work into mainRead it as “on main, merge feature in.” Only main changes. Git then picks one of two kinds of merge, depending on what happened on each branch.
Fast-forward: catching up
Two friends walk the same road. One stops on a bench; the other keeps going. To catch up, the first just walks forward on the same road.
That's a fast-forward. It happens when the current branch hasn't moved since the other one split off. Git slides the branch forward, and no new commit is made:
Before: main is behind feature, on the same line.
After git merge feature: main slides forward to D.
1.Make a branch with two commits
cd ~/Desktop
mkdir merge-lab
cd merge-lab
git init
echo "<h1>Home</h1>" > index.html
git add index.html
git commit -m "Add homepage"
git switch -c feature/about
echo "<h1>About</h1>" > about.html
git add about.html
git commit -m "Add about page"
echo "<p>We build websites</p>" >> about.html
git commit -am "Add about description"
git log --oneline --graph --all* d01cddf (HEAD -> feature/about) Add about description
* 65def6f Add about page
* 298cc62 (main) Add homepage2.Merge it
git switch main
git merge feature/aboutUpdating 298cc62..d01cddf
Fast-forward
about.html | 2 ++
1 file changed, 2 insertions(+)
create mode 100644 about.htmlGit says Fast-forward. Both branches now point at d01cddf, and the history is still a straight line. The branch is done, so delete it:
git branch -d feature/aboutThree-way merge: two rivers joining
Two rivers split from one source, each picks up different things, and later they join into one. That's a three-way merge. It happens when both branches have new commits since they split, so main can't just slide forward.
Git compares three commits:
- The common ancestor, where the branches split.
- The tip of the current branch.
- The tip of the other branch.
It works out what each side changed since the ancestor, combines both, and saves the result as a new merge commit with two parents:
M is the merge commit. Its parents are D (main) and F (feature).
1.Make both branches move
git switch -c feature/contact
echo "<h1>Contact</h1>" > contact.html
git add contact.html
git commit -m "Add contact page"
git switch main
echo "body { font-family: sans-serif; }" > styles.css
git add styles.css
git commit -m "Add base styles"
git log --oneline --graph --all* a44557b (HEAD -> main) Add base styles
| * f0e0e71 (feature/contact) Add contact page
|/
* d01cddf Add about description
* 65def6f Add about page
* 298cc62 Add homepage2.Merge it
git merge feature/contactThe editor opens with the message Merge branch 'feature/contact'. It's fine as it is, so save and close. (--no-edit skips the editor.)
Merge made by the 'ort' strategy.
contact.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 contact.html“ort” is the name of Git's merge method. The graph now shows the rivers joining:
git log --oneline --graph --all* 9a1cbe7 (HEAD -> main) Merge branch 'feature/contact'
|\
| * f0e0e71 (feature/contact) Add contact page
* | a44557b Add base styles
|/
* d01cddf Add about description
* 65def6f Add about page
* 298cc62 Add homepagemain now has both styles.css and contact.html.
3.Look at the merge commit
git show 9a1cbe7commit 9a1cbe7581d50611d8d5924bf3ba08b54414f1bc
Merge: a44557b f0e0e71
Author: Nithin <you@example.com>
Merge branch 'feature/contact'The Merge: line lists two parents. HEAD^1 is the first, the branch that received the merge (a44557b), and HEAD^2 is the second, the branch merged in (f0e0e71).
Side by side
| Fast-forward | Three-way | |
|---|---|---|
| When | Only the other branch moved | Both branches moved |
| What Git does | Slides the branch forward | Combines both sides from the common ancestor |
| New commit | No | Yes, with two parents |
| History | A straight line | A visible join |
Choosing the kind of merge
1.--no-ff: always make a merge commit
git merge --no-ff feature/xEven when a fast-forward is possible, this makes a merge commit, so the history shows which commits were one feature. GitHub's Create a merge commit button for pull requests works the same way.
The feature's commits, B and C, stay grouped, joined to main by the merge commit M.
2.--ff-only: fast-forward or nothing
git merge --ff-only feature/xIf a fast-forward isn't possible, Git refuses instead of making a merge commit, which keeps the history a straight line.
Keeping a feature branch up to date
After a few days on feature/dashboard, teammates have merged ten things into main. Merging main into the feature brings that work in:
git switch feature/dashboard
git merge mainSame rule: the current branch receives the work, and main isn't touched. The feature is tested against the latest code, and merging it back later goes more smoothly.
Undoing a merge
| Situation | Command |
|---|---|
| Not pushed yet | git reset --hard ORIG_HEAD |
| Already pushed | git revert -m 1 <merge-hash> |
| A merge in progress went wrong | git merge --abort |
ORIG_HEADis a bookmark Git sets before a merge: where the branch was before it.-m 1tellsrevertto keep the first parent, the main line, and undo what the merged branch brought in.--abortcancels a merge that stopped partway, for example on a conflict, and puts everything back.
Same golden rule as Lesson 7: reset if it's local, revert if it's shared.
The everyday merge
git switch main # 1. go to the receiving branch
git merge feature/thing # 2. merge the feature in
git log --oneline --graph # 3. check the result
git branch -d feature/thing # 4. clean upOn teams, steps 1 and 2 usually happen on GitHub, through a pull request. Underneath, it's exactly this.
Quick reference
| To… | Run |
|---|---|
| Merge a branch into the current one | git merge branch |
| Merge without the editor | git merge branch --no-edit |
| Always make a merge commit | git merge --no-ff branch |
| Fast-forward or fail | git merge --ff-only branch |
| Update a feature with main | git switch feature, then git merge main |
| Cancel a merge in progress | git merge --abort |
| Undo a local merge | git reset --hard ORIG_HEAD |
| Undo a pushed merge | git revert -m 1 hash |
| See merges in the history | git log --oneline --graph --all |