Custom logic that runs right before you create a Git commit.
- Checks for
FIXMEin any of the staged text files, so that you don't accidentally commit any code that you don't want to.- If it finds any, it stops the commit and tells you which file and line number it was found in, so you can fix the code.
- Checks staged .NET package lock files for
Microsoft.NET.ILLink.Tasks, which is automatically added by Visual Studio and causes package restoration in locked mode to fail because it's not a real dependency of your project.- If it finds any, it automatically removes this dependency from
packages.lock.jsonso thatdotnet restore --locked-modewill succeed. This does not break Visual Studio either, which will either ignore it or try to add it again later. The commit continues automatically. - This dependency is preserved if the project publishes to a single file, for example if
PublishSingleFileorPublishAotare set totruein the.csprojfile.
- If it finds any, it automatically removes this dependency from
- Operating system: Windows (x64 or arm64) or Linux (x64, arm, or arm64)
- Git
- .NET Runtime 9 or later
- Download
GitHooks.zipfrom the latest release. - Locate the
pre-commitexecutable in the subdirectory of the ZIP file which corresponds to your operating system and CPU architecture (e.g.win-x64\pre-commit.exefor Windows x64). - Extract that
pre-commitexecutable to a Git hooks directory, depending on the scope you want it to apply to.- One repository: extract it to
<repo>/git/hooks/pre-commit.exe - All repositories for a user: extract it to any directory, then run
git config --global core.hookspath "<dir>" - All repositories for all users: extract it to any directory, then run
git config --system core.hookspath "<dir>"
- One repository: extract it to
Just run git commit the way you normally would. If it commits successfully, there were no fatal errors.
Otherwise, it will abort the commit and print a message showing why it stopped and how to proceed. This generally involves fixing whatever you commented with FIXME, removing that comment, running git add on that fixed file, and then calling git commit again with your original commit message.
If you want to run this hook without committing anything, you can call
git hook run pre-commit --no-ignore-missing