How to add a pre-commit git hook script using a Gradle task


A step by step guide on adding a pre-commit script to a git repo as part of a Gradle build to guarantee consistency across your team.
Step One: Create a .scripts folder

At the root of your Android Studio project, create a folder called .scripts.
Step Two: Add pre-commit script

In the newly created .scripts folder, create a new script called pre-commit and add any relevant code.
This script will be added to the githooks folder during the gradle build.
If you wish to use our recommended pre-commit script, please use the code below.
Step Three: Add the Task to Gradle

At the project level build.gradle.kts and the following task:
Step Four: Test

Sync the Gradles with your project and confirm that the script appears in your .git/hooks folder.

Once it looks good, commit the changes to your git repo, so that your team can benefit from the script.
Looking to deny commits to specific branches ?
To learn how to block commits to specific branches, follow the tutorial below.