Git Hooks

7 Tools for Writing Safe Git Hook Scripts

Learn how to write safe git hook scripts with seven tools that check shell syntax, format code, and run hooks reliably on every machine.

On this page
  1. Key takeaways
  2. Quick comparison
  3. How to write safe git hook scripts
  4. How we chose
  5. FAQ
  6. Sources

Key takeaways

  • Safe hook scripts start with correct shell syntax, and ShellCheck plus shfmt keep the code checked and consistently formatted.
  • Git itself provides the hooks directory, the sample scripts, and the events you attach your checks to.
  • The official Git hooks documentation is the reference for how each hook behaves, what input it gets, and how to avoid common footguns.
  • Hook managers like pre-commit, husky, and lefthook add structure so your scripts are installed, tested, and shared safely across a team.

Quick comparison

ToolBest forStarting priceFree plan
ShellCheckFinding shell bugs before your hook runsNot includedNot included
shfmtFormatting shell scripts for consistent, readable hooksNot includedNot included
GitThe version control system that runs your hooksNot includedNot included
Git hooksThe official reference for hook behavior and inputsNot includedNot included
pre-commitManaging shared hooks across many languagesNot includedNot included
huskyWiring Node projects into Git events safelyNot includedNot included
lefthookFast, language-agnostic hook managementNot includedNot included

How to write safe git hook scripts

1. ShellCheck

  • Official site: https://www.shellcheck.net
  • Best for: Finding shell bugs before your hook runs
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Catches subtle shell mistakes; runs in editors, CI, and hooks
  • Cons: Only checks shell scripts; advice can need context

ShellCheck is a static analyzer for shell scripts, and it is the first tool to reach for when you write safe git hook scripts. Hooks are often written in bash or sh, where a missing quote or a wrong test operator can silently break the whole script. ShellCheck flags these problems with clear explanations and links to examples.

You can run it from the command line, inside your editor while you type, or as part of CI, so problems are caught at every stage. Because a hook that fails quietly is worse than no hook at all, checking the script itself matters. ShellCheck turns a risky shell script into something you can trust on every machine.

2. shfmt

  • Official site: https://github.com/mvdan/sh
  • Best for: Formatting shell scripts for consistent, readable hooks
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Enforces one clean style; can auto-edit files in place
  • Cons: Does not catch logic bugs; only handles shell syntax

shfmt is a formatter for shell scripts that keeps hook code tidy and consistent. It normalizes indentation, spacing, and line breaks according to a style you choose, so every hook in the repository looks the same. Running shfmt -w on a file rewrites it in place, which makes formatting part of the commit workflow.

Formatting is not the same as correctness, so shfmt pairs naturally with ShellCheck: one cleans the style, the other finds the bugs. In a team, consistent formatting makes hook scripts easier to review and less likely to hide mistakes. For anyone maintaining several shell-based hooks, shfmt is a small tool with a big payoff.

3. Git

  • Official site: https://git-scm.com
  • Best for: The version control system that runs your hooks
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Runs hooks automatically at key events; already on every machine
  • Cons: Hook behavior varies by shell and platform; no built-in validation

Git is the program that actually executes your hooks, so understanding it is part of writing safe git hook scripts. When you commit, push, or merge, Git looks in the .git/hooks directory for a script with the right name and runs it. The hook can then inspect staged files, check messages, or block the action if something is wrong.

Because Git is the foundation, its details shape how hooks behave: the working directory, the environment variables, and the exit code all matter. A hook that returns a non-zero exit code cancels the operation, which is how checks become guardrails. Knowing those basics keeps your scripts predictable across machines.

4. Git hooks

  • Official site: https://git-scm.com/docs/githooks
  • Best for: The official reference for hook behavior and inputs
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Documents every hook type and its arguments; authoritative and free
  • Cons: Dense reading; examples are minimal

The official Git hooks documentation is the reference for how each hook works. It lists every supported hook, from pre-commit and commit-msg to pre-push and post-merge, and explains what arguments and environment each one receives. When a hook misbehaves, this page is where you confirm the expected behavior.

Reading the docs before writing hooks prevents the most common mistakes, such as assuming a hook receives arguments it does not, or forgetting that a hook can block the action. It is free, always current, and shipped with Git itself. For safe hook scripts, start here and keep it bookmarked.

5. pre-commit

  • Official site: https://pre-commit.com/
  • Best for: Managing shared hooks across many languages
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Repeatable config checked into the repo; large hook catalog
  • Cons: Needs Python; some hooks are slower to install

pre-commit adds structure to your hook setup so scripts behave the same on every machine. You declare hooks in a committed config file, and pre-commit installs them, manages their environments, and runs them at the right time. This removes the ad-hoc copying of scripts that leads to broken or stale hooks.

Because the config is versioned, a new teammate gets identical protection after one install step, and updates roll out through normal commits. The shared hook catalog also means you can reuse well-maintained checks instead of writing your own. For teams that want safe, repeatable hooks across languages, pre-commit is a solid foundation.

6. husky

  • Official site: https://typicode.github.io/husky/
  • Best for: Wiring Node projects into Git events safely
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Tiny and fast; hooks live in version control
  • Cons: JavaScript-focused; the .husky directory adds a concept to learn

husky makes Git hooks approachable in Node.js projects by linking npm scripts to Git events. Instead of hand-editing files under .git/hooks, you define your hooks in a .husky directory that is committed with the code. That keeps the hooks visible, reviewable, and versioned alongside the rest of the project.

Because the hooks are plain files that run your existing npm scripts, there is less invisible magic than a fully managed framework. That transparency helps when you need to debug why a hook did or did not fire. For JavaScript teams that want safe, reviewable hooks, husky keeps the setup small and the behavior clear.

7. lefthook

  • Official site: https://lefthook.dev/
  • Best for: Fast, language-agnostic hook management
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Single fast binary; simple YAML config and parallel runs
  • Cons: Smaller ecosystem; parallel output can be harder to read

lefthook manages Git hooks with a single compiled binary and a YAML config file. You map commands to hooks, and lefthook installs and runs them, including parallel execution and glob filters for changed files. Because it has no language runtime of its own, it works in any repository.

That simplicity matters for safe hook scripts: the config is easy to audit, and the tool's exit codes propagate cleanly so a failing check blocks the commit as expected. lefthook also supports skipping hooks explicitly when you need to bypass them, which helps avoid accidental bypasses from stale setup. It is a fast, tidy choice for mixed-language teams.

How we chose

We chose tools that make hook scripts safer in three ways: checking the shell code, formatting it for readability, and managing how hooks are installed and run. Git and its official hooks documentation anchor the list because they define the environment every hook lives in.

We favored tools that reduce the chance of a silent failure, since a hook that does nothing is a false sense of security. Price details are left as Not included because the comparison is about writing safe git hook scripts, not cost.

FAQ

Why do hook scripts need to be checked at all?

A hook script runs at a critical moment, and a small shell bug can make it fail silently or block commits unexpectedly. Checking and formatting the script makes its behavior predictable.

What exit code cancels a Git action?

A hook that exits with a non-zero status cancels the operation, such as a commit or push. That is how a linter or test runner turns a failed check into a blocked commit.

Where do I put my hook scripts?

Git looks in the .git/hooks directory by default, but that folder is not versioned. Managers like husky, pre-commit, and lefthook keep hooks in the repository and install them from there.

Can a hook block a push as well as a commit?

Yes. The pre-push hook runs before a push and can cancel it by returning a non-zero status, which is useful for running tests or checks before code leaves the machine.

Do I need a hook manager to be safe?

No, a single well-written script works, but a manager makes setup repeatable and shared. Without one, hooks are easy to forget on a new machine or after a clone.

Sources