Git pre-commit script for the Google Java Style Guide, using the Google Java Format library
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.

Google Java Format Pre-Commit Hook

A git pre-commit script that formats java code according to the Google Java Style Guide, using the Google Java Format library.


To use this pre-commit script, clone this repo locally, and then link the script from this repo as the .git/hooks/pre-commit script in your java project:

cd ~/my-libraries
git clone
cd ~/my-projects/some-java-project
ln -s ~/my-libraries/gjfpc-hook/ .git/hooks/pre-commit


The script automatically downloads the google-java-format jar from the Google Java Format project to your ~/.local/share/java directory on first run. It then calls this jar to format the entire contents of each staged file with a .java extension when git calls it for pre-commit processing.

If formatting fails, the commit will be aborted. If formatting succeeds, the file will be re-staged (to include any formatting changes in the commit). To avoid committing un-staged hunks, you must skip the script for the commit.


To run the google-java-format jar independently of the pre-commit hook, run the script:

~/my-libraries/gjfpc-hook/ --help

You can also link the formatting script to a local bin directory, either in your home folder:

ln -s ~/my-libraries/gjfpc-hook/ ~/.local/bin/google-java-format

Or another bin directory on your PATH:

sudo ln -s ~/my-libraries/gjfpc-hook/ /local/usr/bin/google-java-format

And then run the script simply as google-java-format:

google-java-format --help


To skip formatting a single commit, use the --no-verify flag (-n for short):

git commit --no-verify

To skip a series of commits -- like when rebasing or merging -- set the NO_VERIFY environment variable in your shell to be not empty, make the commits, and then set NO_VERIFY back to empty:

export NO_VERIFY=1
git rebase -i master
export NO_VERIFY=