Skip to content
A Git pre-commit hook that checks formatting of Go code
Latest commit 0cea757 May 22, 2013 @edsrzf Update alias in README
Closes #3
Failed to load latest commit information.
LICENSE Initial commit Sep 20, 2010 Update alias in README May 22, 2013
fmt-check Remind users to run git add Jan 31, 2013
fmt-fix Switch back to bourne shell for portability. Jan 6, 2013

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.

Something went wrong with that request. Please try again.