Add pre commit check

Add pre commit check


3 min read

I often find myself checking the same thing before making a commit. So it is a nice thing if the computer could run the checking for me. I know we can achieve it by git hooks but I never did it before, so at first, I thought it may be too complicated. But it turns out to be pretty easy. Let's see how to do it.

Suppose we already have a git project. So we have a .git folder in the root. Go to the folder and we should find a subfolder called hooks. List its content, we should see something like this:

$ ls hooks 
applypatch-msg.sample     pre-applypatch.sample     pre-rebase.sample         update.sample
commit-msg.sample         pre-commit.sample         pre-receive.sample
fsmonitor-watchman.sample pre-merge-commit.sample   prepare-commit-msg.sample
post-update.sample        pre-push.sample           push-to-checkout.sample

As you can see, it's a bundle of sample files. They are the hooks we can use. For my case, I want to do some checks before committing, so I should choose a pre-commit hook. Let's see the content of the file pre-commit.sample.

# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
# To enable this hook, rename this file to "pre-commit".

if git rev-parse --verify HEAD >/dev/null 2>&1
        # Initial commit: diff against an empty tree object
        against=$(git hash-object -t tree /dev/null)

# If you want to allow non-ASCII filenames set this variable to true.
allownonascii=$(git config --type=bool hooks.allownonascii)

# Redirect output to stderr.
exec 1>&2

# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
        # Note that the use of brackets around a tr range is ok here, (it's
        # even required, for portability to Solaris 10's /usr/bin/tr), since
        # the square bracket bytes happen to fall in the designated range.
        test $(git diff --cached --name-only --diff-filter=A -z $against |
          LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0
        cat <<\EOF
Error: Attempt to add a non-ASCII file name.

This can cause problems if you want to work with people on other platforms.

To be portable it is advisable to rename the file.

If you know what you are doing you can disable this check using:

  git config hooks.allownonascii true
        exit 1

# If there are whitespace errors, print the offending file names and fail.
exec git diff-index --check --cached $against --

As you can see, it is just a plain script file, we can write in any scripting language like bash/zsh/fish as we like.

We can run commands inside. If we want the git commit to fail, we should return a non-zero status code. For me, I want to run tsc to check typescript errors and also run npm run lint to run the lint rules I have. So I can have a simple hook file like this.


npx tsc
npm run lint

As you can see, it's extremely simple. Then we can make commits like before then git will do the checking for us.