Version note: references describe SonarQube Server 2025.2 behaviour. Check your server, scanner and edition requirements. These examples were not run against a live SonarQube server.
What this guide covers
SonarQube can apply checks to changed code rather than every old issue in a project. The new-code definition sets the starting point for that comparison.
Before you start
Use a project with at least one completed analysis. You need project administration access. The available comparison options depend on the edition and analysis type.
Step 1: Choose a comparison
Open the project’s New Code settings. Choose a supported option that matches your work, such as a reference branch or previous project version. Write down what the chosen option compares. A pull-request comparison uses its target branch.
Step 2: Check the Git checkout
Run these commands in the scanner’s checkout:
git rev-parse HEAD
git status --short
git rev-parse --is-shallow-repository
git log -5 --oneline
A shallow checkout has only part of the repository history. Use a full checkout when the scanner needs that history. Fetch the intended reference branch before comparing against it.
Step 3: Set a version only for releases
If you use the previous-version option, set a meaningful release version in sonar-project.properties:
sonar.projectKey=orders-service
sonar.projectVersion=2.4.0
sonar.sources=src
sonar.tests=test
sonar.sourceEncoding=UTF-8
Change the example paths and project key. Do not change the version on every build: that changes the comparison point.
Step 4: Verify with a small change
Analyze a test branch with a short, known change. Compare the changed files locally:
git rev-parse HEAD
git rev-parse --is-shallow-repository
git diff --stat origin/main...HEAD
git diff --numstat origin/main...HEAD
Then open the SonarQube analysis and check which lines it marks as new. If the comparison differs, check the base branch, Git history and scanner settings.
Check the result
You should be able to explain why each changed line is counted. Do not change the comparison just to make a failing gate pass.
Conversation
0 commentsNo comments yet. Start the conversation below.