This is my workflow for using StyleCop.Analyzers in a repository, without needing my entire team to use the tool. At a high level, I install StyleCop, use it, and then remove it before pushing. This gives me the benefit of the analyzer, without pushing the analyzer on my team. Win win!

First, finish the current task and commit.

cd my-repository
git commit -m "Finish work on task"

Second, install StyleCop in every project in the repository.

Do that with a Directory.Build.props file in the repository root.


    <PackageReference Include="StyleCop.Analyzers" Version="1.1.118"/>


Alternatively, if that does not work, you can add it to every project like this with PowerShell.

Get-ChildItem -Recurse *.csproj `
| Select-Object -ExpandProperty FullName `
| % { dotnet add $_ package StyleCop.Analyzers }

Third, run StyleCop by building the solution

dotnet build --no-incremental 

The --no-incremental flag makes sure that the build runs even when none of the files have changed. Without that, the Analyzer will not run, given our current configuration.

Fourth, make changes and commit

Read the warnings and make as many "corrections" as you would like to the code. Once you're done, add only the C# files, commit, and push.

git add *.cs
git commit -m "Respond to code analysis suggestions"
git push 

Fifth, remove StyleCop.

git reset --hard HEAD
git clean -xfd 

Finally, tweak the rules.

Here is an example of how to do that.