Ora

How do I change code suggestions in GitHub?

Published in GitHub Pull Request Review 4 mins read

To effectively "change code suggestions" in GitHub, you primarily utilize the platform's inline suggestion feature within pull request reviews. This allows you to propose specific code modifications that the original author can apply with a single click.

Understanding Code Suggestions in GitHub

In the context of GitHub, "code suggestions" refer to proposed changes to specific lines of code within a pull request. These are not AI-driven suggestions (like those from GitHub Copilot, which are configured separately in your IDE or GitHub settings), but rather direct, actionable code edits offered by reviewers during the code review process. This feature streamlines feedback by letting you provide exact code snippets as alternatives.

Key Aspects of GitHub Code Suggestions

  • Actionable: Suggestions appear as diffs that the pull request author can commit directly.
  • Contextual: They are tied to specific lines of code in the "Files changed" tab of a pull request.
  • Part of Code Review: They are typically submitted as part of a comprehensive pull request review.

How to Make and Modify Code Suggestions

Making a code suggestion in a pull request involves a few straightforward steps. You can suggest changes to existing lines or propose entirely new blocks of code.

1. Initiating a Code Suggestion

  1. Navigate to the Pull Request: Open the pull request you wish to review.
  2. Access "Files changed": Go to the Files changed tab to view the code differences.
  3. Identify the Line: Hover your mouse over the specific line of code where you want to suggest a change.
  4. Click the Suggestion Icon: A blue + icon will appear to the left of the line number. Click this icon.
  5. Edit the Suggestion Block: An editable suggestion block will appear in the comment field below the code line. The original line(s) will be pre-filled. Edit the text within this suggestion block to propose your desired code modification. This is where you "change" the suggested code.
  6. Add Explanatory Comment (Optional): In the comment field, you can also type additional text to explain why you are suggesting this change.
  7. Submit as Part of Review: Choose to Add single comment or Start a review if you have more feedback.

2. Commenting Without a Suggestion

If you want to provide general feedback on a line without proposing a direct code change:

  • Line-specific comment: Follow steps 1-3 above, but instead of editing the suggestion block, just type your comment directly into the text field.
  • File-level comment: To comment directly on an entire file, locate the file in the "Files changed" tab. To the right of the file name, click the ... (ellipsis) button or the comment icon and type your comment.

3. Editing a Pending Suggestion

If you have created a suggestion but have not yet submitted your review, you can still modify it:

  1. Locate Your Pending Comment: Scroll to your comment block containing the suggestion.
  2. Click the "Edit" Icon: An edit icon (pencil) will typically be available on your pending comment.
  3. Modify the Code: Re-open the suggestion block and edit the text within the suggestion block to adjust your proposed code.
  4. Save Changes: Click "Update comment" or similar to save your modifications.

Once a suggestion is submitted as part of a review, it becomes immutable. If you need to "change" a submitted suggestion, you would typically add a new comment or suggestion explaining the further modification, or the author might simply choose not to apply your original suggestion and instead make their own modification.

4. Applying and Interacting with Suggestions

As a pull request author, receiving suggestions is straightforward:

  1. View Suggestions: In the "Files changed" tab of your pull request, you will see suggestions embedded in the review comments.
  2. Review the Diff: Each suggestion shows a small diff of the proposed change.
  3. Apply Suggestion: To accept a suggestion, click the Commit suggestion button directly below the suggestion block. This will create a new commit on your branch with the suggested changes.
  4. Modify or Dismiss: You can also choose to modify the suggested code further before committing, or simply dismiss the suggestion if you prefer a different approach.

Best Practices for Code Suggestions

  • Be Clear: Always provide context for your suggestions in the accompanying comment.
  • Keep it Concise: Focus suggestions on single, logical changes.
  • Consider Impact: Think about the implications of your suggested changes on the existing codebase.
  • Utilize Markdown: Format your comments and suggestions with Markdown for readability (e.g., code blocks, bold text).

By leveraging GitHub's robust review features, including inline code suggestions, teams can collaborate more effectively, enhance code quality, and streamline the development workflow.