8.6 KiB
stage | group | info | type |
---|---|---|---|
Create | Code Review | To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/product/ux/technical-writing/#assignments | index, reference |
Suggest changes (FREE)
- Introduced custom commit messages for suggestions in GitLab 13.9 with a flag named
suggestions_custom_commit
. Disabled by default.- Generally available in GitLab 13.10. Feature flag
suggestions_custom_commit
removed.
Reviewers can suggest code changes with a Markdown syntax in merge request diff threads. The merge request author (or other users with the appropriate role) can apply any or all of the suggestions from the GitLab UI. Applying suggestions adds a commit to the merge request, authored by the user who suggested the changes.
Create suggestions
-
On the top bar, select Main menu > Projects and find your project.
-
On the left sidebar, select Merge requests and find your merge request.
-
On the secondary menu, select Changes.
-
Find the lines of code you want to change.
- To select a single line:
- Hover over the line number, and select Add a comment to this line ({comment}).
- To select multiple lines:
- Hover over the line number, and select Add a comment to this line ({comment}).
- Select and drag your selection until all desired lines are included. To learn more, see Multi-line suggestions.
- To select a single line:
-
In the comment toolbar, select Insert suggestion ({doc-code}). GitLab inserts a pre-populated code block into your comment, like this:
```suggestion:-0+0 The content of the line you selected is shown here. ```
-
Edit the pre-populated code block to add your suggestion.
-
Select either Start a review or Add to review to add your comment to a review, or Add comment now to add the comment to the thread immediately.
Multi-line suggestions
Changed in GitLab 13.11: suggestions in multi-line comments also become multi-line.
Reviewers can also suggest changes to multiple lines with a single suggestion within merge request diff threads by selecting and dragging selection to all relevant line numbers or by adjusting the range offsets. The offsets are relative to the position of the diff thread, and specify the range to be replaced by the suggestion when it is applied.
In the previous example, the suggestion covers three lines above and four lines below the commented line. When applied, it would replace from 3 lines above to 4 lines below the commented line, with the suggested change.
NOTE: Suggestions for multiple lines are limited to 100 lines above and 100 lines below the commented diff line. This allows for up to 200 changed lines per suggestion.
Apply suggestions
The merge request author can apply suggested changes directly from the merge request:
- On the top bar, select Main menu > Projects and find your project.
- On the left sidebar, select Merge requests and find your merge request.
- Find the comment containing the suggestion you want to apply.
- To apply suggestions individually, select Apply suggestion.
- To apply multiple suggestions in a single commit, select Add suggestion to batch.
- Optional. Provide a custom commit message to describe your change. If you don't provide a custom message, the default commit message is used.
- Select Apply.
After a suggestion is applied:
- The suggestion is marked as Applied.
- The comment thread is resolved.
- GitLab creates a new commit with the changes.
- If the user has the Developer role, GitLab pushes the suggested change directly into the codebase in the merge request's branch.
Nest code blocks in suggestions
To add a suggestion that includes a fenced code block, wrap your suggestion in four backticks instead of three:
````suggestion:-0+2
```shell
git config --global receive.advertisepushoptions true
```
````
Configure the commit message for applied suggestions
GitLab uses a default commit message when applying suggestions. This message
supports placeholders, and can be changed. For example, the default message
Apply %{suggestions_count} suggestion(s) to %{files_count} file(s)
renders
like this if you apply three suggestions to two different files:
Apply 3 suggestion(s) to 2 file(s)
Merge requests created from forks use the template defined in the target project.
To meet your project's needs, you can customize these messages and include other placeholder variables:
- On the top bar, select Main menu > Projects and find your project.
- On the left sidebar, select Settings > Merge requests.
- Scroll to Merge suggestions, and alter the text to meet your needs. See Supported variables for a list of placeholders you can use in this message.
Supported variables
The template for commit messages for applied suggestions supports these variables:
Variable | Description | Output example |
---|---|---|
%{branch_name} |
The name of the branch to which suggestions were applied. | my-feature-branch |
%{files_count} |
The number of files to which suggestions were applied. | 2 |
%{file_paths} |
The paths of the file to which suggestions were applied. Paths are separated by commas. | docs/index.md, docs/about.md |
%{project_path} |
The project path. | my-group/my-project |
%{project_name} |
The human-readable name of the project. | My Project |
%{suggestions_count} |
The number of suggestions applied. | 3 |
%{username} |
The username of the user applying suggestions. | user_1 |
%{user_full_name} |
The full name of the user applying suggestions. | User 1 |
For example, to customize the commit message to output
Addresses user_1's review
, set the custom text to
Addresses %{username}'s review
.
Batch suggestions
- Introduced in GitLab 13.1 as an alpha feature with a flag named
batch_suggestions
, disabled by default.- Enabled by default in GitLab 13.2.
- Generally available in GitLab 13.11. Feature flag
batch_suggestions
removed.- Introduced custom commit messages for batch suggestions in GitLab 14.4.
You can apply multiple suggestions at once to reduce the number of commits added to your branch to address your reviewers' requests.
-
To start a batch of suggestions to apply with a single commit, select Add suggestion to batch:
-
Add as many additional suggestions to the batch as you wish:
-
To remove suggestions, select Remove from batch:
-
Having added all the suggestions to your liking, when ready, select Apply suggestions. You can optionally specify a custom commit message for batch suggestions (GitLab 14.4 and later) to describe your change. If you don't specify it, the default commit message is used.
WARNING: Suggestions applied from multiple authors creates a commit authored by the user applying the suggestions.