Photo by Steve Johnson on Unsplash

Improve your Development Workflow using global git hooks

Sher Chowdhury
8 min readApr 29, 2023

--

Git hooks are a useful way to automatically highlight/fix issues before you submit your code changes for review. For example git hooks can stop you accidentally including secrets (e.g. passwords) in your commits.

There are different types of git hooks, pre-commit, commit-msg, pre-push…and etc, of which the pre-commit hooks are arguable the most popular type. These get run whenever a user runs git commit. A commit gets aborted if a pre-commit hook exits with a non-zero exit code, although you can bypass that with git commit --no-verify.

In this article I’m going to set up 2 pre-commit hooks, they are:

  1. A hook that prevents me making commits directly on the master (or main) branch. This hook is useful for me, because sometimes I forget to create a new dev branch before committing my changes. Worse still, sometimes I even ended up pushing code up directly to the main branch. You can configure github branch protection rules to prevent this from happening, but if you’re the git repo’s admin, then those branch protection rules get ignored.
  2. A hook that prevents me from accidentally including secrets (e.g. passwords) in my commits.

Creating Custom hooks

--

--