# Configuring new code calculation

By default, your project applies the baseline for new code (global-level new code definition). This means it consistently uses the baseline for new code. Consequently, any modifications to the baseline will automatically be applied to your project. You can apply a specific new code definition for your project instead.

By default, any branch in your project applies the project’s new code definition. You can apply a specific new code option instead.

For more information, see [about-new-code](https://docs.sonarsource.com/sonarqube-server/2025.2/user-guide/about-new-code "mention").

## Setting a specific new code definition for your project or branch <a href="#setting-specific-new-code-definition-for-project" id="setting-specific-new-code-definition-for-project"></a>

As a project administrator, you can set the new code definition for your project or branch in the UI (except the Specific analysis option) or using the Web API, at creation time or anytime later as explained below.

{% hint style="info" %}
For more compliance with the Clean as You Code methodology, the Specific analysis option can only be set using the Web API, as it would require frequent user action to be kept up to date.
{% endhint %}

{% hint style="info" %}
You can configure the Reference branch option through an analysis parameter on the CI/CD host. See **Additional setup and recommendations > If using Reference branch option** below.
{% endhint %}

### In the UI <a href="#in-the-ui" id="in-the-ui"></a>

To set a specific new code definition for your project or branch in the UI:

1\. Retrieve your project.

2\. Go to **Project Settings > New Code**.

3\. In **Choose the baseline for new code for this project**, select **Define a specific setting for this project** and then the option you want to apply to your project.

<figure><img src="https://3577027091-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F69lEOGGgOhCpumODGD9v%2Fuploads%2Fgit-blob-e4234bd9a6edde9f58fd310968bdf27a9f8e0f32%2Fsonarqube-server-new-code-definition-for-your-project-ui.png?alt=media" alt="Defining a specific setting for the project&#x27;s new code definition"><figcaption></figcaption></figure>

4\. To apply a specific setting to a branch:

* Navigate to the **Set a specific setting for a branch** section at the bottom of the page.
* In the **Actions** column of the branch, select the pen icon. The **New Code for \<branch>** dialog opens.
* Select the new code option you want to apply to the branch.
* Select **Save**.

<figure><img src="https://3577027091-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F69lEOGGgOhCpumODGD9v%2Fuploads%2Fgit-blob-1d3b58a378eba6e80f6e8eb2aa019ff31ae4a702%2Fsonarqube-server-set-new-code-option-at-branch-level.png?alt=media" alt="Changing new code definition for a specific branch"><figcaption></figcaption></figure>

### Via the Web API <a href="#via-the-web-api" id="via-the-web-api"></a>

Use the [api/new\_code\_periods/set](https://next.sonarqube.com/sonarqube/web_api/api/new_code_periods/set) endpoint by specifying:

* To set a specific definition for your project: the project
* To set a specific definition for a branch: the project and branch.

## Additional setup and recommendations <a href="#additional-setup-and-recommendations" id="additional-setup-and-recommendations"></a>

Make sure to follow the recommendations about the [verifying-code-checkout-step](https://docs.sonarsource.com/sonarqube-server/2025.2/analyzing-source-code/scanners/scanner-environment/verifying-code-checkout-step "mention") and [scm-integration](https://docs.sonarsource.com/sonarqube-server/2025.2/analyzing-source-code/scm-integration "mention").

### If using any option except Reference branch <a href="#if-using-any-option-except-reference-branch" id="if-using-any-option-except-reference-branch"></a>

When using any new code option other than Reference branch, we recommend completing your merges using the fast-forward option without a merge commit; examples include GitHub’s squash and merge or rebase and merge options. That way, blame for merged commits will always have a more recent commit date.

### If using Previous version option <a href="#if-using-previous-version-option" id="if-using-previous-version-option"></a>

The current version of a project is determined in different ways depending on the build system:

* If the analysis is done using the [sonarscanner-for-maven](https://docs.sonarsource.com/sonarqube-server/2025.2/analyzing-source-code/scanners/sonarscanner-for-maven "mention"), then SonarQube Server reads the version from the `pom.xml` file.
* If the analysis is done with the [sonarscanner-for-gradle](https://docs.sonarsource.com/sonarqube-server/2025.2/analyzing-source-code/scanners/sonarscanner-for-gradle "mention") then SonarQube Server reads the version from the `build.gradle` file.
* In all other cases, you must explicitly specify the version by setting the analysis parameter `sonar.projectVersion`.

### If using Reference branch option <a href="#if-using-reference-branch-option" id="if-using-reference-branch-option"></a>

To make the Reference branch option work properly:

* The same branch name should be used in the SCM repository and SonarQube.
* The reference branch is fetched in the CI/CD host’s local repository (This is usually done through the cloning of the remote repository by the CI pipeline).
* The code in the CI/CD host’s local repository matches the code in the remote repository.

You can use the `sonar.newCode.referenceBranch` property to apply the Reference branch option to the analysis of a branch, overriding the global and project-level new code definition set in the UI. This property specifies the reference branch value.

This setting is particularly useful during the first analysis when the branch to be analyzed does not exist yet in SonarQube (i.e. you cannot set a new code definition for this branch in SonarQube).

## Related pages <a href="#related-pages" id="related-pages"></a>

* [about-new-code](https://docs.sonarsource.com/sonarqube-server/2025.2/user-guide/about-new-code "mention")
* [setting-new-code-definition-at-global-level](https://docs.sonarsource.com/sonarqube-server/2025.2/instance-administration/analysis-functions/setting-new-code-definition-at-global-level "mention")
