Skip to content

Instantly share code, notes, and snippets.

@kurnakovv
Created November 3, 2025 07:30
Show Gist options
  • Select an option

  • Save kurnakovv/54bc4b9d158ad700b64e471ec0545272 to your computer and use it in GitHub Desktop.

Select an option

Save kurnakovv/54bc4b9d158ad700b64e471ec0545272 to your computer and use it in GitHub Desktop.
Run dotnet format only for current git branch changes | PowerShell script
$baseBranch = "main" # or "master"
$currentBranch = git rev-parse --abbrev-ref HEAD
# Get changed file names in current branch
$files = git diff --name-only $baseBranch
# Delete useless spaces
$files = $files | Where-Object { $_ -ne "" }
if ($files.Count -eq 0) {
Write-Host "No diff between current branch ($currentBranch) and base branch ($baseBranch)"
exit
}
Write-Host "Diff between current branch ($currentBranch) and base branch ($baseBranch) list"
Write-Host "---------------------"
$files | ForEach-Object { Write-Host $_ }
Write-Host "---------------------"
Write-Host "Running formatting..."
Write-Host "---------------------"
Write-Host "dotnet format --include $files --verbosity detailed --no-restore"
Write-Host "---------------------"
dotnet format --include $files --verbosity detailed --no-restore
@kurnakovv
Copy link
Author

kurnakovv commented Nov 3, 2025

Description

This script allows you to run dotnet format only for those files that have been changed in the git branch

Instruction

For example, you created a branch based on the main branch, then changed something and committed it.

git checkout -b feature-branch
git add .
git commit -m "Change something"

After this, running the script, it will find all files that have been changed relative to the base branch (even if you have not committed them) and will start formatting

PS D:\eProjects\DotnetAnalyzyHabr> .\DotnetFormat-GitBranchChanges.ps1
Diff between current branch (feature-branch) and base branch (main) list
---------------------
DotnetAnalyzyHabr.WebAPI/Controllers/TestController.cs
---------------------
Running formatting...
---------------------
dotnet format --include DotnetAnalyzyHabr.WebAPI/Controllers/TestController.cs --verbosity detailed --no-restore
---------------------
The dotnet runtime version is '10.0.0-rc.2.25502.107'.
Formatting code files in workspace 'D:\eProjects\DotnetAnalyzyHabr\DotnetAnalyzyHabr.sln'.
Project DotnetAnalyzyHabr.WebAPI is using configuration from 'D:\eProjects\DotnetAnalyzyHabr\.editorconfig'.
Project DotnetAnalyzyHabr.WebAPI is using configuration from 'D:\eProjects\DotnetAnalyzyHabr\DotnetAnalyzyHabr.WebAPI\obj\Debug\net9.0\DotnetAnalyzyHabr.WebAPI.GeneratedMSBuildEditorConfig.editorconfig'.
Project DotnetAnalyzyHabr.WebAPI is using configuration from 'C:\Program Files\dotnet\sdk\10.0.100-rc.2.25502.107\Sdks\Microsoft.NET.Sdk\analyzers\build\config\analysislevel_9_default.globalconfig'.
Project DotnetAnalyzyHabr.Tests is using configuration from 'D:\eProjects\DotnetAnalyzyHabr\.editorconfig'.
Project DotnetAnalyzyHabr.Tests is using configuration from 'D:\eProjects\DotnetAnalyzyHabr\DotnetAnalyzyHabr.Tests\obj\Debug\net9.0\DotnetAnalyzyHabr.Tests.GeneratedMSBuildEditorConfig.editorconfig'.
Project DotnetAnalyzyHabr.Tests is using configuration from 'C:\Program Files\dotnet\sdk\10.0.100-rc.2.25502.107\Sdks\Microsoft.NET.Sdk\analyzers\build\config\analysislevel_9_default.globalconfig'.
Project DotnetAnalyzyHabr.BL is using configuration from 'D:\eProjects\DotnetAnalyzyHabr\.editorconfig'.
Project DotnetAnalyzyHabr.BL is using configuration from 'D:\eProjects\DotnetAnalyzyHabr\DotnetAnalyzyHabr.BL\obj\Debug\net9.0\DotnetAnalyzyHabr.BL.GeneratedMSBuildEditorConfig.editorconfig'.
Project DotnetAnalyzyHabr.BL is using configuration from 'C:\Program Files\dotnet\sdk\10.0.100-rc.2.25502.107\Sdks\Microsoft.NET.Sdk\analyzers\build\config\analysislevel_9_default.globalconfig'.
D:\eProjects\DotnetAnalyzyHabr\DotnetAnalyzyHabr.WebAPI\Controllers\TestController.cs(1,1): error WHITESPACE: Fix whitespace formatting. [D:\eProjects\DotnetAnalyzyHabr\DotnetAnalyzyHabr.WebAPI\DotnetAnalyzyHabr.WebAPI.csproj]
Running 5 analyzers on DotnetAnalyzyHabr.WebAPI.
Running 219 analyzers on DotnetAnalyzyHabr.WebAPI.
Formatted code file 'D:\eProjects\DotnetAnalyzyHabr\DotnetAnalyzyHabr.WebAPI\Controllers\TestController.cs'.
Formatted 1 of 20 files.
Format complete in 6792ms.

Links

If you need a script that runs formatting only for currently git modified files, here it is
If you need to run formatting on save, here it is

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment