Sub topic 3
Create a merge commit
This chapter assumes you can commit, branch, push, and read git log output, and that you have opened and reviewed a pull request on GitHub. Here you will learn what the "Create a merge commit" button adds to the base branch and how to spot it afterward.
What the method does
"Create a merge commit" is GitHub's default merge method. It keeps every commit from the pull request branch unchanged and adds one new commit that joins the two lines of work. That new commit is a merge commit, and it has two parents:
- The first parent is the previous tip of the base branch (for example
main). - The second parent is the tip of the pull request branch.
The original commits keep their hashes, authors, and messages. Nothing is rewritten.
In the diagram, M is the merge commit. Its first parent is B and its second parent is D.
Reproducing it locally
GitHub's button is equivalent to a merge with --no-ff. This option forces Git to create a merge commit even when a fast-forward would be possible.
git switch main
git merge --no-ff feature -m "Merge pull request #123 from team/feature"GitHub's own message has the form Merge pull request #123 from owner/branch, followed by the pull request title on the next line. You can edit both fields in the web UI before you confirm.
Seeing it in the history
Run this on the base branch after the merge:
git log --oneline --graph --decorate mainA merge commit shows up as a join in the graph:
* e4f5a6b (HEAD -> main) Merge pull request #123 from team/feature
|\
| * 9c8d7e6 Add validation
| * 3b2a1f0 Add form
|/
* 7a6b5c4 Previous work on mainThe feature commits stay visible as a side branch. You can also list only merge commits with git log --merges, or hide them with git log --no-merges.
Why teams choose it
- It keeps the full record of how the work happened, including each commit on the branch.
- The branch commits keep their original hashes. Anyone who already has a copy of the branch still shares history with
main, so there is no reconciliation step afterward. - The pull request has one clear join point. That makes a later revert simple, as the [Reverting a merged pull request](#) chapter shows.
Costs to know about
- The history is not linear. Tools that expect a straight line, and the "Require linear history" branch protection rule, do not accept this method.
- Every pull request adds an extra commit. On busy repositories the graph fills with merge commits and parallel lines.
- Messy branch commits such as "fix typo" or "wip" stay in
mainpermanently.
Common errors
- Reverting a merge commit without
-m. Git refuses, because it cannot tell which parent is the mainline. Usegit revert -m 1 <merge-commit-sha>to keep the base branch as the mainline. - Merging the base branch into the feature branch repeatedly. Each "Merge branch 'main' into feature" commit then arrives in
maintoo, which adds noise. Rebase the feature branch instead if the team prefers a cleaner graph. - Expecting the button to be available. A repository administrator can disable this method in settings, in which case the button does not appear.
Exercise
A pull request with three commits is merged with "Create a merge commit". How many new commits does the base branch gain, and how many parents does the newest one have?
Show answer
Four. The three original commits arrive unchanged, and one new merge commit joins them. The merge commit has two parents: the previous tip of the base branch and the tip of the pull request branch.
Exercise
Which command shows only the merge commits on main, and which command reverts one of them safely?
Show answer
git log --merges main lists them. git revert -m 1 <merge-commit-sha> reverts one, keeping the first parent (the base branch) as the mainline.