Sub topic 7
Squash Commit Messages and Authorship
A squash merge replaces the commits of a pull request with one new commit on the base branch. That commit needs a message and an author, and GitHub fills in both for you. This chapter covers where those defaults come from, how to change them, and what you lose if you accept them without looking.
This chapter assumes you have read the previous chapter on squash and merge. You should also be comfortable reading git log output with full commit details.
What the squash commit contains
The squash commit has these properties:
- Tree: the final state of the pull request's changes, applied on top of the base branch.
- Parent: a single parent, the previous tip of the base branch.
- Message: built from the repository's default, then editable in the merge box.
- Author: normally the person who opened the pull request.
- Committer: GitHub itself, shown as
GitHub <[email protected]>.
The original commits stay on the pull request page and on the head branch. They never become ancestors of the new commit. The squash commit's message is the only text about the pull request's history that reaches the base branch, so its content matters.
How the default message is built
The default depends on a repository setting. Under Settings, then General, then Pull Requests, the option Allow squash merging has a dropdown for the default commit message. A maintainer with admin access sets it.
| Setting | Commit title | Commit body |
|---|---|---|
| Default message | The commit title if the pull request has one commit. Otherwise the pull request title. | The commit body if there is one commit. Otherwise a bulleted list of every commit message. |
| Pull request title | The pull request title | Empty |
| Pull request title and commit details | The pull request title | A bulleted list of every commit message |
| Pull request title and description | The pull request title | The pull request description |
In every case GitHub appends the pull request number to the title, for example Add retry logic (#123). That suffix links the commit back to the pull request. Changelog tools and git log --grep "#123" both use it.
The diagram shows how the choices lead to a message.
Why the default is often noisy
With the default setting and several commits, the body is a list such as:
Add retry logic (#123)
* wip
* fix typo
* address review comments
* add test for timeoutThat list is a record of how you worked, not of what the change does. Messages like wip stay in the history of the base branch for good.
Two choices avoid this:
- Set the default to Pull request title and description. The pull request description is something authors already write for reviewers, and it can be edited at any time before merging.
- Keep the default and edit the message at merge time.
The first choice makes the pull request description the long-term record. Teams that choose it should agree on a description template.
Editing the message when you merge
Click the arrow on the merge button and select Squash and merge. GitHub shows editable title and description fields. Whatever you leave in those fields becomes the commit message.
Three details matter:
- Edits apply to this merge only. The repository default stays the same.
- The pull request page does not change. The title and description you edited in the merge box are not written back to the pull request.
- The pull request number is added to the title when you merge. If you remove it from the field, you lose the link to the pull request.
Authorship and co-authors
The author field identifies who wrote the change. The committer field identifies who created the commit object. For a squash merge on GitHub the two differ. You can see this with a custom format:
git log -1 --format='author: %an <%ae>%ncommitter: %cn <%ce>%n%n%B' mainTypical output:
author: Maria Ortiz <[email protected]>
committer: GitHub <[email protected]>
Add retry logic (#123)
* add retry helper
* add test for timeout
Co-authored-by: Dev Patel <[email protected]>Several people may have committed to the pull request branch, but a commit has only one author. GitHub keeps the others visible with Co-authored-by trailers. A trailer is a Key: value line at the end of a commit message, after a blank line. GitHub reads Co-authored-by: Name <email> and shows those people as co-authors on the commit page. The commit also counts toward their contribution graph, provided the email address is linked to their account.
With the default message settings that include commit details, GitHub adds trailers for the other commit authors automatically. If you rewrite the body by hand, check that the trailers are still there.
You can also add co-authors yourself. If a reviewer rewrote half of a change in a suggestion commit, add a line like this to the merge box:
Co-authored-by: Dev Patel <[email protected]>Signatures
Commits made through the web merge button are signed by GitHub, so they show as Verified. That holds even when the original commits on the branch were unsigned. The signature vouches that GitHub created the commit. It does not say the author's key signed the change. If your branch protection requires signed commits, a squash merge through the web satisfies it. A local squash does not, unless you sign the commit yourself.
Setting the message from the command line
The gh CLI takes the title and body as flags, so you can write the message without opening the merge box:
gh pr merge 123 --squash \
--subject "Add retry logic to the HTTP client (#123)" \
--body "Retries idempotent requests up to three times with exponential backoff.
Co-authored-by: Dev Patel <[email protected]>"If you omit --subject and --body, GitHub applies the repository default. The --subject value is used exactly as given, so include the (#123) suffix yourself if you want it.
Squashing locally
If you squash outside GitHub, the message is your responsibility. git merge --squash stages the combined changes without creating a commit. It also writes a draft message, made of the branch's commit messages, to .git/SQUASH_MSG. Running git commit with no -m opens your editor with that draft.
git switch main
git merge --squash feature
git commitThe author is whoever runs git commit, not the original author of the branch's commits. GitHub is not the committer, and no signature is added unless you configure signing. Use --author or trailers to credit others:
git commit --author="Maria Ortiz <[email protected]>" \
-m "Add retry logic" \
-m "Co-authored-by: Dev Patel <[email protected]>"Each -m adds a paragraph, so the trailer lands after a blank line.
Common mistakes
- Accepting a
wiplist. Fix it with the default setting Pull request title and description, or edit the message at merge time. - Overwriting the pull request number. Without
(#123)in the title, finding the pull request fromgit logtakes extra work. - Losing trailers. Check the end of the message before you merge.
- Expecting per-commit authorship. After a squash,
git blamepoints at the squash commit and its single author. Credit for the other people lives only in the trailers. - Describing the wrong change. If review changed the approach, the pull request title may be outdated. Fix it before merging, because it becomes the commit title.
Exercise
A repository uses the default squash setting "Default message". A pull request has three commits, titled "wip", "fix test", and "rename helper". Its title is "Add retry logic" and its number is 57. What title and body will the squash commit have if you merge without editing? Which setting would give a cleaner body, and what would that body contain?
Show answer
The title is "Add retry logic (#57)". The body is a bulleted list of the three commit messages: "wip", "fix test", and "rename helper", plus Co-authored-by trailers for any other authors. Setting the default to "Pull request title and description" gives a cleaner body, because the body would be the pull request description that the author wrote for reviewers.
Exercise
A squash commit on main shows author "Maria Ortiz" and committer "GitHub". Dev Patel pushed two of the five commits on the branch. How can you tell from the commit message that Dev contributed, and what must be true of the message for GitHub to treat it that way?
Show answer
The message should end with a "Co-authored-by: Dev Patel <email>" line. It must be a trailer: the last paragraph of the message, separated from the text above by a blank line, with no other text after it. The email should be linked to Dev's GitHub account for the credit to appear on the commit page and the contribution graph.