Skip to main content

Pre-commit Hook

CSharpier can be used with a pre-commit hook to ensure that all staged files are formatted before being committed.


Install pre-commit via your preferred Python package manager. Run pre-commit install to install the Git hook scripts.


CSharpier runs as part of MegaLinter's pre-commit hooks.


If you prefer not to run the other linters included in MegaLinter, you can alternatively run CSharpier as a local pre-commit hook.

Add the following to your .pre-commit-config.yaml:

- repo: local
- id: dotnet-tool-restore
name: Install .NET tools
entry: dotnet tool restore
language: system
always_run: true
pass_filenames: false
- commit
- push
- post-checkout
- post-rewrite
description: Install the .NET tools listed at .config/dotnet-tools.json.
- id: csharpier
name: Run CSharpier on C# files
entry: dotnet tool run dotnet-csharpier
language: system
- c#
description: CSharpier is an opinionated C# formatter inspired by Prettier.


From the root of your repository

dotnet tool install husky
dotnet husky install

Optionally - add this to one of your projects to automate the install for future developers

<!-- set HUSKY to 0 in CI/CD disable this -->
<Target Name="husky" BeforeTargets="Restore;CollectPackageReferences" Condition="'$(HUSKY)' != 0">
<Exec Command="dotnet tool restore" StandardOutputImportance="Low" StandardErrorImportance="High"/>
<Exec Command="dotnet husky install" StandardOutputImportance="Low" StandardErrorImportance="High"
<!-- update this to be the root of your solution -->
WorkingDirectory="../../" />

Modify the file at .husky/task-runner.json

"tasks": [{
"name": "Run csharpier",
"command": "dotnet",
"args": [ "csharpier", "${staged}" ],
"include": [ "**/*.cs" ]

You can run and test your task with the following command.

dotnet husky run

Once you are sure the task is working properly, you can add it as a pre-commit hook.

dotnet husky add pre-commit -c "dotnet husky run"

If you want the pre-commit hook to be opt in, ignore the .husky/pre-commit file. It can be enabled by individual developers if the run the command above.