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.

Terminal
git switch main        # the branch that receives the work
git merge feature      # bring feature's work into main

Read 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:

A
Bmain
C
Dfeature

Before: main is behind feature, on the same line.

A
B
C
Dmainfeature

After git merge feature: main slides forward to D.

1.Make a branch with two commits

Terminal
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
Output
* d01cddf (HEAD -> feature/about) Add about description
* 65def6f Add about page
* 298cc62 (main) Add homepage

2.Merge it

Terminal
git switch main
git merge feature/about
Output
Updating 298cc62..d01cddf
Fast-forward
 about.html | 2 ++
 1 file changed, 2 insertions(+)
 create mode 100644 about.html

Git says Fast-forward. Both branches now point at d01cddf, and the history is still a straight line. The branch is done, so delete it:

Terminal
git branch -d feature/about

Three-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:

  1. The common ancestor, where the branches split.
  2. The tip of the current branch.
  3. 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:

A
B
C
D
Mmain
E
Ffeature

M is the merge commit. Its parents are D (main) and F (feature).

1.Make both branches move

Terminal
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
Output
* a44557b (HEAD -> main) Add base styles
| * f0e0e71 (feature/contact) Add contact page
|/
* d01cddf Add about description
* 65def6f Add about page
* 298cc62 Add homepage

2.Merge it

Terminal
git merge feature/contact

The editor opens with the message Merge branch 'feature/contact'. It's fine as it is, so save and close. (--no-edit skips the editor.)

Output
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:

Terminal
git log --oneline --graph --all
Output
*   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 homepage

main now has both styles.css and contact.html.

3.Look at the merge commit

Terminal
git show 9a1cbe7
Output
commit 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-forwardThree-way
WhenOnly the other branch movedBoth branches moved
What Git doesSlides the branch forwardCombines both sides from the common ancestor
New commitNoYes, with two parents
HistoryA straight lineA visible join

Choosing the kind of merge

1.--no-ff: always make a merge commit

Terminal
git merge --no-ff feature/x

Even 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.

A
Mmain
B
C

The feature's commits, B and C, stay grouped, joined to main by the merge commit M.

2.--ff-only: fast-forward or nothing

Terminal
git merge --ff-only feature/x

If 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:

Terminal
git switch feature/dashboard
git merge main

Same 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

SituationCommand
Not pushed yetgit reset --hard ORIG_HEAD
Already pushedgit revert -m 1 <merge-hash>
A merge in progress went wronggit merge --abort
  • ORIG_HEAD is a bookmark Git sets before a merge: where the branch was before it.
  • -m 1 tells revert to keep the first parent, the main line, and undo what the merged branch brought in.
  • --abort cancels 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

Terminal
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 up

On 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 onegit merge branch
Merge without the editorgit merge branch --no-edit
Always make a merge commitgit merge --no-ff branch
Fast-forward or failgit merge --ff-only branch
Update a feature with maingit switch feature, then git merge main
Cancel a merge in progressgit merge --abort
Undo a local mergegit reset --hard ORIG_HEAD
Undo a pushed mergegit revert -m 1 hash
See merges in the historygit log --oneline --graph --all