Hone

Lessons · Git · when both sides moved

When both sides moved

If both branches have new commits, git merge creates a merge commit with two parents: the join point of the two lines of work.

Hone is a place to practise programming. This is one of its lessons, written out in full and free to read without an account.

What it is for

The merge commit is what makes the history a graph rather than a line. It is also why reverting a merge needs -m, and why rebasing exists for people who prefer straight lines.

How to think about it

After merging, look at git log --graph to see the shape, and git log -1 --format=%P to see the two parents. A merge commit with a conflict resolution is the one to read most carefully.

Worked example

git switch -c topic && echo t > t.txt && git add t.txt && git commit -q -m "Add t"
One commit on topic.
git switch main && echo m > m.txt && git add m.txt && git commit -q -m "Add m"
And one on main: both moved.
git merge --no-edit topic
No fast-forward possible: Git makes a merge commit.
git log -1 --format=%P
Two hashes: the merge commit's two parents.

Your turn

Show the parents of the last commit.

git log -1 --format=

The trap

A merge commit with two parents cannot be reverted without -m 1 to say which parent is the mainline.

Practise when both sides moved on HoneA question on it now, a coding challenge where there is one, and it is remembered for review. Free, no email needed.