Created
November 3, 2025 07:30
-
-
Save kurnakovv/54bc4b9d158ad700b64e471ec0545272 to your computer and use it in GitHub Desktop.
Run dotnet format only for current git branch changes | PowerShell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $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 |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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
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