Git pre-push hook’s use case; Catch lint failures before pushing code to remote repo

Recently I ended up adding this simple pre-push hook to my local git repo. In our work environment, when we make a build, Bamboo is building the branch to make sure it is not failing. We have a setup where we take lint warnings very seriously and lint errors can end up in a PR, failing to build.

giphy

If you’re using AndroidStudio’s build in Version Control, you get notified about lint errors but if you’re using the Terminal to make commits, this can be a problem.

1- From your project root, go to hooks directory.

~ $ cd .git/hooks/

2- Now add the above script into pre-push

~ $ nano pre-push

Add following script into file.

#!/bin/sh
echo "executing pre-push hook to make sure lint is not failing the build"

# Run the lint with the Gradle wrapper. You can use lintProductionRelease 
# to run it for only one flavour. (time saving)
./gradlew lint

# Store the last exit code in a variable
RESULT=$?

# Return the exit code
exit $RESULT

3- Make sure the script is executable.

~ $ chmod +x .git/hooks/pre-commit

Now every time you will make a push from Terminal/AndroidStudio and there is a lint error, it will not push the branch to remove repo.