How to change the main branch in SonarQube?

Hi, we have the same problem. For some reason, sonarcloud thinks master is the main branch, and ignoes our develop branch and it ignores all our PRs against the develop branch. We dont have admin access to the UI in our org.

I have read all the replies, and it seems we need to do something like this:

sonar.branch.name=develop

every time anyone runs the tool, or it will produce wrong results.

The command line we use is this:

expoet SONAR_TOKEN=xxxx
mvn verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=ourpro_our-service

How do you specify the branch name?

Assuming this is possible, how do we get sonar to treat this as the main branch?

currently, when we run the toll on PR branches on develop, we dont see anything under branches or PRs in the UI.

This info really should be in a tutorial.

Hi @nutmix

export SONAR_TOKEN=xxxx
mvn verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=ourpro_our-service -Dsonar.branch.name=<SomeBranchName>

Should do the trick.

Note that in many CI environments the branch name is auto detected so you should not even have to specify the branch name if the CI integration is done as per Sonar recommendations.

Once you have multiple branches analyzed, in recent enough versions of SonarQube (10.x or the 2025.1 LTA), you may select the one that’s you main branch from the SonarQube UI as per the screenshot below.

1 Like

Thanks. According to the Pull request analysis | SonarQube Cloud Documentation docs, sonarcloud should automatically detect pull requests, but it doesnt.

According to this docs: Analysis Parameters | SonarQube Cloud Documentation it says you can specify the branch and target branch:

  • sonar.branch.name = branch-1
  • sonar.branch.target = main

But says this should not be done for pull requests, so we are kind of stuck.

Maybe we do need to put the PR branch as branch name in every call, so rather than

sonar.branch.name = develop

we actually need

sonar.branch.name = feature/123-my_PR
sonar.branch.target = develop

?

Really, all we want to do is get sonar to ignore master, and consider develop as the main branch. It would have been great if there was a way to do this out of the box, but for now we have to find a way to hack it.

A CI wont magically detect or use a branch name, the CI will simply call the command line you indicate in setup page you are given when you first hit “configure analysis”, namely:

mvn verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar -Dsonar.projectKey=xxx

Really, this document should include the required params as well, as its impossible to deduce these. As arguably noone will want to analyze PRs against master, the branch should be in the example.

Of course we can make our build tool pass any params we need, but we need to know what should be passed to get up and running.

Are there any other prams we need to know about?

When we tried using

sonar.branch.name = staging (which is our equivalent of develop)

It shows up in the UI as a short lived branch, and there is no option to make the the main branch, so we don’t have a solution yet unfortunately.

Hi,

I’ve covered this in one of your other threads.

There are multiple reasons for that. One is that this thread is about SonarQube Server and (as I’m aware from some of your other threads) you’re using SonarQube Cloud. The options vary. This is one feature that hasn’t made the jump from one product to the other.

You need to edit your branch pattern.

And since staging was initially created as a short-lived branch, you’ll need to delete it and re-create it after you’ve corrected the pattern. Branch types are immutable.

When you re-create it, make sure to do so without passing a branch name parameter. This means it will be created as main in SonarCloud. You can then rename it to staging.

And BTW, you’ve resurrected a 3-month old thread here. Per the FAQ, please don’t do that.

If there’s more to this that you’d like to pursue, please create a new thread.

 
Ann

Thanks for the reply, but we are still no closer to figuring out how to change the main branch to staging. Even if we create a new long lived branch, I assume this will still not solve our problem that we cant get any PRs into sonarcloud, presumably because it thinks master is the main branch.

I dont think you have covered this in other threads. we havent seen any info on this. If we cant do the steps on the command line, we cant automate it in a build pipline

how do we delete it? There is no button or option in the UI that we can find.

We certainly cant delete the branch from our git repo, that would cause untold chaos.

Hi,

Are you a project admin?

If so, in the list of branches, you see a button at the end of the row. That will give you the options for the branch.

 
Ann