A Git pre-commit hook that checks formatting of Go code
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


This project contains two scripts intended to be used as git pre-commit hooks.

The first, fmt-check, prevents you from committing if you need to run gofmt on any modified .go file.

The second, fmt-fix, runs all modified .go files through gofmt before committing. NOTE: This script does not play well when adding partial changes, such as with git add -p or git commit -p.

You should use either one or the other, not both.

Both scripts are originally based on a golang-nuts discussion.

To install in a single repository, copy a script to .git/hooks/pre-commit and make sure it's executable.

You might also find it useful to create a git alias for gofmt by running:

git config --global alias.gofmt '!echo $(git diff --cached --name-only --diff-filter=ACM | grep '.go$') | xargs gofmt -w -l | xargs git add'

Then you can run git gofmt and format all modified .go files. Note that this alias will not handle situations where partial file changes have been added to the index.