How to run Spotless and ktlint in a pre-commit git hook

A step by step guide on adding a pre-commit git script that runs Spotless and ktlint formatting before committing code to an Android Studio project.

Oscar de la Hera Gomez
Written by Oscar de la Hera Gomez
First published on 06/08/2023 at 15:37
Last Updated on 06/08/2023 at 17:03
Two flowers that represent Kotlin and Android Studio. Beneath them sits the text "Run Spotless and ktlint in pre-commit git script."

A step by step guide on adding a pre-commit git script that runs Spotless and ktlint formatting before committing code to an Android Studio project.

SubscribeDownload Open Source Kotlin Starter Project

Please note that this tutorial assumes that you have setup your project to run with Spotless and ktlint. If you do not, please complete the tutorial below before proceeding.

How to add ktlint and spotless to an Android project

Step One: Duplicate the pre-commit.sample script

A screenshot of Finder highlighting the location of the pre-commit.sample git hooks script.

Open your Finder and duplicate the pre-commit.sample script found under .git/hooks/pre-commit.sample.

You can duplicate the script by pressing cmd + d on a Mac.

Step Two: Rename the pre-commit script

A screenshot of Finder showing the pre-commit script after being renamed.

Rename the duplicated pre-commit.sample script to pre-commit.

Step Three: Replace code

A screenshot of textedit showing the code provided below within the pre-commit script.

Open the pre-commit script in text edit and replace the code with the one below.

This code:

  • Gathers the staged files - to make sure that only changes from Spotless from these files are saved to the git repository.
  • Runs Spotless Apply
  • In the event that Spotless succeeded, the script adds any changes to the staged files to the git repository.

Step Four: Test

A screenshot of Sourcetree showing the pre-commit script being ran during a commit.

Please note that the projectsEvaluated Tasks are custom tasks that we have added.

Add some innocent tab spacing and commit code.

You will see that the script runs and that the code is fixed within the commit.

Curious how to run tasks during the Gradle build phase ?

To learn how to run custom tasks during the Gradle build phase, consult the tutorials below.

How to run a Gradle Kotlin DSL pre-build task in Android StudioHow to run a task at any stage of an app Gradle build

Want to make this script consistent across your team?

We recommend following the tutorial below to learn how to add this script to your githooks folder during a Gradle build, enabling code consistency across teams.

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

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
SubscribeContact UsVisit our BlogView our ServicesView our Work

Partner with us

We would love to get to know you and see how we can help your organization with its goals and needs.
Let's Talk

Stay Informed

Get occasional updates about our company, research, and product launches.
Subscribe