Git Hooks

7 Tools for Formatting and Linting Code in Hooks

Learn how to format and lint code in hooks with these seven tools that catch mistakes and keep your repository clean before every commit.

On this page
  1. Key takeaways
  2. Quick comparison
  3. How to format and lint code in hooks
  4. How we chose
  5. FAQ
  6. Sources

Key takeaways

  • Git hooks run scripts before commits, pushes, or merges, and they are the natural place to format and lint code automatically.
  • A hook framework such as pre-commit or husky makes hook setup repeatable across a team and keeps the config in version control.
  • Linters like ESLint and ShellCheck catch bugs early, while formatters keep every file consistent from one commit to the next.
  • Pairing a hook framework with lint-staged keeps runs fast by limiting checks to the files you actually changed.

Quick comparison

ToolBest forStarting priceFree plan
pre-commitRunning many language-specific hooks from one configNot includedNot included
huskyHooking npm scripts and Node projects into Git eventsNot includedNot included
lefthookFast polyglot hooks with simple YAML configNot includedNot included
overcommitRuby projects that want a full hook workflowNot includedNot included
lint-stagedLimiting lint and format runs to staged filesNot includedNot included
ESLintFinding and fixing problems in JavaScript and TypeScriptNot includedNot included
ShellCheckCatching mistakes in shell scripts used inside hooksNot includedNot included

How to format and lint code in hooks

1. pre-commit

  • Official site: https://pre-commit.com/
  • Best for: Running many language-specific hooks from one config
  • Starting price: Not included
  • Free plan: Not included
  • Pros: One config file for many languages; large catalog of ready-made hooks
  • Cons: Needs Python on the machine; some hooks are slow on large repos

pre-commit is a framework for managing Git hook scripts written in Python. You list each hook in a .pre-commit-config.yaml file, and the tool installs them into the .git/hooks directory. Because the config is committed to the repository, every teammate gets the same checks with one command. It works across languages, so a single project can run a JSON formatter, a Python linter, and a secret scanner together.

What makes pre-commit useful is its catalog of shared hooks. Many projects publish reusable hooks that format files, check syntax, or reject leftover debug statements. Hooks can also run in isolated environments, which keeps dependencies tidy. If you want one entry point for formatting and linting, pre-commit is a solid place to start.

2. husky

  • Official site: https://typicode.github.io/husky/
  • Best for: Hooking npm scripts and Node projects into Git events
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Small and fast; fits naturally into package.json workflows
  • Cons: Tied mostly to JavaScript projects; the .husky folder needs careful setup

husky connects your npm scripts to Git hooks, which makes it popular in JavaScript and TypeScript teams. Instead of writing shell scripts by hand, you map commands like npm run lint to hooks such as pre-commit or pre-push. The setup is a few lines in your package.json or a dedicated .husky directory, and the hooks are installed automatically when dependencies are added.

The appeal of husky is simplicity. It stays out of the way, runs fast, and works well with lint-staged for limiting checks to changed files. Since hooks live in the repository, a new developer gets the same protection after running npm install. For Node projects that already have scripts for formatting and linting, husky is a natural fit.

3. lefthook

  • Official site: https://lefthook.dev/
  • Best for: Fast polyglot hooks with simple YAML config
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Single binary and fast parallel execution; language-agnostic
  • Cons: Smaller ecosystem than some rivals; parallel output can be harder to read

lefthook is a Git hooks manager written in Go that ships as a single binary. You define hooks in a lefthook.yml file, and lefthook runs the matching commands when Git fires the event. Because it is a compiled tool, it starts quickly and can run several jobs at the same time, which matters in large repositories where hook latency adds up.

lefthook works with any language, so it suits monorepos and teams that mix Python, Go, Ruby, and JavaScript. It supports glob filters, per-command environments, and scripts written inline, so most formatting and linting tasks map onto it directly. Teams that want fast hooks without pulling in a language runtime often pick lefthook for that reason.

4. overcommit

  • Official site: https://github.com/sds/overcommit
  • Best for: Ruby projects that want a full hook workflow
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Rich built-in checks and message hooks; deep Git integration
  • Cons: Adds a Ruby dependency; configuration has a learning curve

overcommit is a Git hooks manager aimed at Ruby projects, though its checks are not limited to Ruby. You enable hooks in a .overcommit.yml file, and the tool manages the hook files under .git/hooks for you. It goes beyond simple commands, offering checks for commit messages, whitespace, and a wide set of linters with sensible defaults.

A notable feature is that overcommit can verify commit messages against rules, which pairs well with teams that follow a commit convention. It can also warn about unsigned commits or outdated dependencies through optional checks. The cost is that you need Ruby available and a little time to learn the configuration. For Ruby shops, that tradeoff is usually acceptable.

5. lint-staged

  • Official site: https://github.com/lint-staged/lint-staged
  • Best for: Limiting lint and format runs to staged files
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Keeps hooks fast by only touching changed files; easy glob config
  • Cons: Not a hook runner by itself; needs another tool or script to call it

lint-staged solves a specific problem: running linters and formatters over an entire project on every commit is slow. Instead, lint-staged takes the list of staged files and passes only those files to the commands you configure. A typical setup runs ESLint and Prettier on changed JavaScript files while leaving the rest of the repository untouched.

It is commonly paired with husky, since lint-staged is not a hook manager on its own. You define tasks with file globs and command mappings in your package.json, then call lint-staged from a pre-commit hook. The result is fast, focused checks that still prevent broken or messy code from being committed.

6. ESLint

  • Official site: https://eslint.org
  • Best for: Finding and fixing problems in JavaScript and TypeScript
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Deep plugin ecosystem; autofix and custom rules
  • Cons: Configuration can grow complex; slower on very large codebases

ESLint is a linter for JavaScript and TypeScript that analyzes code for errors and style problems. It parses your files, applies a set of rules you choose, and reports anything that breaks them. Many issues can be fixed automatically with the --fix flag, which makes it a common choice inside pre-commit hooks.

The real strength of ESLint is its plugin system. Teams extend it with rules for frameworks like React, Vue, or Angular, and share their configs through packages. While the rule set can take time to tune, the payoff is consistent code and bugs caught before review. When wired into a hook, ESLint gives every commit the same baseline of quality.

7. ShellCheck

  • Official site: https://www.shellcheck.net
  • Best for: Catching mistakes in shell scripts used inside hooks
  • Starting price: Not included
  • Free plan: Not included
  • Pros: Finds subtle shell bugs; runs in editor, CI, and hooks
  • Cons: Only analyzes shell scripts; does not format code

ShellCheck is a static analysis tool for shell scripts. Many Git hooks are written in bash or sh, and small mistakes in those scripts can silently disable a whole hook. ShellCheck flags problems like unquoted variables, incorrect test syntax, and command misuse before the script runs on a teammate's machine.

Because hooks are often the last line of defense before a push, their own correctness matters. ShellCheck can run inside a pre-commit setup, in CI, or in an editor while you write the script. It pairs well with a formatter such as shfmt, since one checks the logic and the other cleans the style. For any team that writes shell-based hooks, ShellCheck is worth keeping in the pipeline.

How we chose

We picked tools that solve distinct parts of the same job: running hooks, choosing what to run, and doing the actual formatting and linting. Each entry is actively maintained and widely used, and the list covers the main languages and workflows teams hit in practice.

We favored tools that store configuration in the repository so every developer gets identical behavior, and we included both general hook managers and the specific linters that do the checking. Price details are left as Not included because these are all open-source or free tools where paid plans are not the point of the comparison.

FAQ

What is a Git hook?

A Git hook is a script that Git runs automatically at points like before a commit or after a push. You can use hooks to format and lint code, run tests, or check commit messages without asking developers to remember extra steps.

Can I run hooks on Windows and macOS?

Yes, most hook managers work across operating systems, though shell-based hooks need a compatible shell. Tools like husky and pre-commit handle platform differences so the same config works for the whole team.

Do formatters and linters do the same thing?

No. A formatter changes code layout, such as spacing and line breaks, while a linter finds likely bugs and style problems. They are often used together inside the same pre-commit hook.

Will hooks slow down my commits?

Hooks add some time, but tools like lint-staged keep the delay small by only checking files you changed. Running the same checks in parallel, as lefthook does, also helps on larger projects.

Do I need to install these on every machine?

The tool itself must be available on each machine, but the configuration lives in the repository. Most projects add a setup step so a single install command gets hooks ready for new developers.

Sources