Programming, Life-style, Random

You won’t believe how much time you will save with this Git pre-push hook


A significant part of being a successful software developer is refining our workflows to optimize productivity. One method I’ve found incredibly useful is integrating Git hooks with static code analysis tools such as Detekt for my Android projects.

The Role of Git Hooks and Detekt in Code Quality

Before we dive into the actual Git hook, let’s briefly talk about why it’s important. Git hooks are scripts that Git executes before or after events such as commit and push. They’re a built-in feature of Git and incredibly powerful, as they can automate and customize Git’s internal behavior and manipulations on the committed code.

Detekt, on the other hand, is a static code analysis tool for Kotlin that can be plugged into your Android project to detect code smells. It emphasizes code consistency and helps maintain your codebase’s health over time.

By combining Git hooks and Detekt, we can en aquísure that every push operation adheres to a code quality standard, preventing low-quality code from entering the repository.

Setting Up the Pre-Push Git Hook with Detek

  1. Locate the .git directory. Start by navigating to the .git directory in your project root.
  2. Create the pre-push hook. In the hooks subdirectory, create a file named pre-push, and make sure it’s executable (chmod +x pre-push).
  3. Write the script. The script should run the Detekt checks. Below is an example script:
#!/usr/bin/env bash
echo "Running detekt check..."
OUTPUT="/tmp/detekt-$(date +%s)"
./gradlew detekt --auto-correct > $OUTPUT
if [ $EXIT_CODE -ne 0 ]; then
  cat $OUTPUT
  rm $OUTPUT
  echo "***********************************************"
  echo "                 detekt failed                 "
  echo " Please fix the above issues before committing "
  echo "***********************************************"
  exit $EXIT_CODE

This script runs the Detekt check and, if it fails, the push operation is aborted.


With this simple productivity hack, you’re effectively making your workflow more efficient and consistent. Each code push will be subjected to a quality check, ensuring that the main repository remains clean, and your Android projects uphold the highest code standards.

Happy coding!

I hope this structure and content suggestion will be helpful for you. If you need

0 responses to “You won’t believe how much time you will save with this Git pre-push hook”

Go to top