Sub topic 7
Suggesting changes directly in a review
This chapter assumes you can open a pull request, read its diff, and leave a line comment. It covers suggested changes, which let a reviewer propose an exact replacement that the author applies with one click.
What a suggestion is
A normal review comment describes a problem and leaves the fix to the author. A suggested change includes the fix. The reviewer writes the replacement text in a special block inside the comment, and GitHub renders it as a diff with an Apply suggestion button.
Suggestions fit small, mechanical edits:
- Typos and wording in docs or comments
- A renamed variable or a corrected constant
- A missing null check or a one-line bug fix
- Formatting that a linter would flag
For larger changes, write a normal comment and let the author decide how to restructure the code.
Writing a suggestion
- In the Files changed tab, hover over a line and click the blue + to start a comment. To cover several lines, drag the + down the range.
- Click the Add a suggestion button in the comment toolbar (the icon shows a plus and minus). GitHub inserts a fenced block with the info string
suggestionand pre-fills it with the current lines. - Edit the text inside that block so it holds the lines as they should read after the change. Add an explanation above the block.
- Click Start a review or Add review comment to save it. You can also use Add single comment to post it immediately.
If the line currently reads const timeoutMs = 10000;, the body of the suggestion block is only the replacement line:
const timeoutMs = 30000;The block replaces the whole selected range. Select exactly the lines you want replaced. If you select one line and write three lines in the block, the author gets three lines in place of one.
Applying suggestions
The author sees the suggestion as a green and red diff under the comment. They have two options:
- Apply suggestion creates a commit on the pull request branch right away.
- Add suggestion to batch collects several suggestions, and Commit suggestions applies them together in one commit.
Batching keeps the history tidy and runs CI once instead of once per suggestion. Applied suggestions are committed under the author's account and credit the reviewer as co-author. The conversation is marked outdated.
Common errors
- No Apply button. You cannot apply suggestions on a closed pull request, on a pull request that is queued to merge, or when the suggestion's lines no longer exist in the diff. Suggestions also cannot be applied while the review is still pending, because the reviewer has not submitted it yet.
- Wrong range. Replacing the wrong lines is the usual mistake. Check the rendered diff before you submit.
- Whitespace drift. Indentation inside the block must match your project's style, or the applied commit will fail a linter.
- Forks. The author can apply suggestions only if the branch is writable. For fork pull requests, this works when the author allows maintainer edits.
- Stale suggestions. If new commits change the same lines, the suggestion becomes outdated and may not apply cleanly.
Exercise
A reviewer selects line 12 of a file and writes a suggestion block containing three lines. What happens when the author clicks Apply suggestion, and what should the reviewer have done if they only wanted to add one line?
Show answer
Line 12 is replaced by all three lines, so the commit adds two net lines and removes the original. If the reviewer only wanted to insert a line, the block should repeat the original line 12 and add the new line next to it. The reviewer should also check the rendered diff before submitting.