Sub topic 7
Squash Commit Messages and Authorship
This chapter assumes you know basic Git (commit, branch, push, pull), can open and review a pull request on GitHub, and can read git log output. It also assumes you know what "Squash and merge" does to the commit graph. Here we look at the one commit it creates: who is credited for it and what its message says.
What GitHub writes by default
When you click Squash and merge, GitHub builds a single new commit on the base branch. It fills in the title and body for you, and you can edit both before you confirm.
The default depends on how many commits the pull request has, plus a repository setting.
| Pull request contents | Default title | Default body |
|---|---|---|
| One commit | That commit's subject | That commit's body |
| Several commits | The pull request title, with the number appended, such as `Add feature (#123)` | A bulleted list of the commit messages |
Repository admins can change this under Settings, then General, then Pull Requests. The setting for squash merging lets you choose the default commit message from the pull request title and description, the pull request title only, or the commit messages. The exact options on offer can change over time, so check the dropdown in your repository.
Who is the author
The squash commit has one author and one committer.
- Author: the person who opened the pull request.
- Committer: GitHub itself, shown as
GitHub <[email protected]>. The commit is signed by GitHub's key when signing applies.
The other contributors do not vanish. GitHub appends a Co-authored-by: trailer for each additional person who committed to the branch. A trailer is a Key: value line at the end of a commit message that Git and GitHub both understand.
Add feature (#123)
* Add parser
* Handle empty input
* Fix typo in error message
Co-authored-by: Sam Rivera <[email protected]>GitHub uses these trailers to show several avatars on the commit and to count the commit toward each person's contribution graph.
Check the result
After the merge, look at the commit on the base branch. This command shows author, committer, and the full message.
git log -1 --format=fuller mainfuller prints both Author and Commit lines. For a squash merge you will usually see the pull request author on the first and GitHub on the second.
Editing the message before merging
The message dialog is your last chance to clean up the history. Two habits help.
- Replace the generated bullet list with a short summary of what changed and why. Lines like "fix typo" and "address review comments" say nothing to a reader of
maina year from now. - Keep the
Co-authored-by:lines at the end of the message. If you delete them, the credit disappears. If you edit the body, make sure the trailers stay in their own block after a blank line.
The title is where the (#123) suffix comes from. It links the commit back to the pull request, which is where the review discussion lives. Removing it makes that link harder to find.
Common errors
- Assuming each commit keeps its own credit. After a squash merge,
git logshows one entry. Only the trailers preserve the other contributors. - Losing trailers when pasting a hand-written message. The merge box accepts whatever you type. Trailers you remove are gone.
- Using the wrong default for your team. If you search history by commit message, the "pull request title only" default hides detail that "commit messages" would keep. If your commit messages are noisy, the reverse is true.
- Expecting
git blameto show the original author per line. After a squash, blame points to the squash commit. To see the original commits, open the pull request.
Exercise
A pull request has three commits by Ana and two by Ben. A maintainer squashes it and replaces the whole message with "Add search". Who is credited, and how could the maintainer have kept credit for both?
Show answer
Ana, the pull request author, is the commit author, and GitHub is the committer. Ben receives no credit because the generated Co-authored-by: Ben <email> trailer was deleted. The maintainer should have kept the trailer at the end of the message, in its own block after a blank line.