Sub topic 3
Create a merge commit
This chapter assumes you can commit, branch, push, and pull with Git. It also assumes you have opened and reviewed a pull request on GitHub, and that you can read git log output. You should know that a branch is a movable pointer to a commit and that every commit points to its parent.
What the method does
"Create a merge commit" is the default option on the merge button. It joins the pull request's branch (the head branch) into the branch it targets (the base branch) by adding one new commit to the base. That commit is the merge commit, and it has two parents:
- The first parent is the previous tip of the base branch.
- The second parent is the tip of the head branch.
None of the head branch's commits are rewritten. They keep their SHAs, authors, timestamps, and messages, and they become reachable from the base branch through the second parent. This is the same result as running git merge --no-ff locally. The --no-ff flag tells Git to create a merge commit even when a fast-forward would be possible.
GitHub never fast-forwards on the web. If the base has not moved since you branched, the merge button still creates a merge commit.
What ends up on the base branch
After the merge, the base branch contains these commits:
- Every commit that was on the base branch before.
- Every commit from the head branch (B and C in the diagram), with their original SHAs.
- The merge commit M, whose tree is the combined result of both lines of work.
The base branch moves from D to M. The head branch itself does not move, so you can delete it after the merge without losing any commits. They stay reachable through M.
The default commit message
GitHub fills in the merge commit message for you:
- The title is
Merge pull request #123 from owner/feature-branch. - The body is the pull request title.
Before you confirm the merge, the web UI lets you edit both the title and an extended description. Many teams use this to put a meaningful summary in the body, because the default title only names the branch.
For authorship, the commit author is the person who clicked the merge button. The committer is GitHub itself, and the commit is signed by GitHub. The original commits keep their own authors. Anyone who contributed commits to the head branch stays visible in the history.
Merging conflicts and staleness
GitHub can create a merge commit only if the head and base merge cleanly. If they conflict, the merge button is disabled until you resolve the conflicts. You can do that in the web editor or locally. Locally, you merge the base into the head branch and push:
git switch feature
git fetch origin
git merge origin/main
# resolve conflicts, then:
git commit
git push origin featureThis adds a merge commit to the head branch before the pull request is merged. When the pull request is merged afterward, the result has two merge commits. The first one pulls main into the feature, and the second one pulls the feature into main. That extra commit is a known cost of the method, and it is one reason some teams prefer squash or rebase merges.
The head branch does not need to be up to date with the base for the merge to work. It only needs to be mergeable. A branch protection rule named "Require branches to be up to date before merging" adds the stricter requirement.
Seeing the merge in the log
The quickest way to see a merge commit is the graph view of the base branch:
git log --oneline --graph --decorate mainA pull request merged with a merge commit appears as a fork and a join:
* 3f9a1c2 (HEAD -> main, origin/main) Merge pull request #123 from octo/feature
|\
| * 8d2b7e4 Add validation to the form
| * 1c5e9a0 Add form component
* | 7a4f6d3 Fix typo in README
|/
* 52e0b18 Previous workThe |\ line is where the history forks, and |/ is where it rejoins. The merge commit is at the top with two lines leaving it.
The merge commit also lets you read the history at two levels of detail:
# Only the merge commits and direct commits on main: one line per pull request
git log --oneline --first-parent main
# The full history, including every commit inside each pull request
git log --oneline main--first-parent follows only the first parent of each commit. Since the first parent of a merge commit is the previous base tip, this view lists pull requests as single entries. git blame --first-parent and git bisect --first-parent use the same idea.
Doing the same from the command line
You can request a merge commit from the gh CLI. The flags --subject and --body replace the default message:
gh pr merge 123 --merge
gh pr merge 123 --merge --subject "Add form validation (#123)" --body "Validates email and phone fields."You can reproduce the same result locally. This is useful for understanding what GitHub does, but a local merge pushed to main does not mark the pull request as merged by GitHub's workflow unless the pushed commits match. In practice, use the button or gh.
git switch main
git merge --no-ff feature -m "Merge pull request #123 from octo/feature"Reverting a merge commit
A merge commit is one commit, so you can undo the whole pull request with a single revert. Git needs to know which parent to treat as the mainline, because a merge commit has two sides:
git revert -m 1 <merge-commit-sha>-m 1 means "keep the first parent's side", which is the base branch. The revert commit then removes the changes the head branch introduced. GitHub's "Revert" button on a merged pull request does this for you and opens a new pull request.
Without -m, Git stops with an error like commit ... is a merge but no -m option was given.
There is a trap in the next step. After you revert a merge, Git considers the head branch's commits to be already merged. If you fix the branch and open a new pull request with the same commits, the new merge brings in only the new commits. The reverted changes do not come back. To restore them, either revert the revert commit first or, better, put the fixes on a new branch and re-apply the original work on top.
Costs and benefits
The following table summarizes the method.
| Aspect | Merge commit |
|---|---|
| Original commits | Preserved, with original SHAs |
| New commits on base | One (the merge commit) |
| History shape | Non-linear, with forks and joins |
| Revert a whole pull request | One `git revert -m 1` |
| Works with "Require linear history" | No, the rule blocks it |
| Follow-up branches built on the head | Stay valid, no rebase needed |
The last row matters in practice. Because the original commits are kept, anyone who branched from your feature branch can merge or rebase onto main without conflicts caused by duplicated changes. The squash and rebase methods lack this property, and a later chapter covers that pitfall.
The costs are a noisier graph and, on busy repositories, many commits whose only content is the join. If your team reads history with --first-parent, that noise mostly disappears.
Common errors
- "Merge commit" is missing from the merge button. An administrator turned it off in the repository settings, or the branch protection rule "Require linear history" is on. A later chapter covers both.
git revertcomplains about-m. You are reverting a merge commit. Add-m 1.- The pull request merged, but the changes seem to be missing after a revert and re-merge. See the warning above.
- The log looks tangled. Use
git log --first-parent mainfor a one-line-per-pull-request view.
Exercise
A pull request was merged with "Create a merge commit" as commit M on main. Which are M's two parents, and which command undoes the whole pull request in one commit?
Show answer
The first parent is the tip of main before the merge, and the second parent is the tip of the pull request's head branch. The command is git revert -m 1 <M>, where -m 1 selects the first parent (main's side) as the mainline.
Exercise
You reverted the merge commit of pull request #123, then fixed a bug on the same branch and opened a new pull request. After merging it, the original feature's changes are still missing from main. Why, and how do you get them back?
Show answer
After the revert, Git still treats the original head commits as already merged into main, so the new merge brings in only the commits added after that point. To restore the original changes, revert the revert commit (or re-apply the original work on a new branch) and then merge the fixes.