November 18, 2018
Git hooks are customizable shell scripts that will be executed during life cycle of a Git command. If you like to do some cleanups like auto code formatting, or just to trigger a deploy or rebuild after every commit, Git hooks are here to accelerate your workflow.
push command so that we keep our remote repo always tidy. The script itself can be pretty simple,
#!/bin/sh # make this script exit immediately if one command exits with a non-zero status set -e # register file contents in the working tree to the index git update-index -g # execute commands exec npm run eslint .
We should save the script above as
.git/hooks to hook it up with the pre-push process. After that, a
git push will quit early if command
npm run eslint . “exits with a non-zero status” - that is, exits with an error or exception.
One problem with Git hooks is that since they live inside
.git directory, they are not versioned and will not be pushed or cloned automatically. We can certainly do some manual work to add those hook scripts to
.git/hooks every time when cloning a repo, but one way to save some hassle is to use a hook manager like Husky.
Happy Git fishing with Git hooks!