Skip to content

Git Hooks

In this lesson you will learn hooks in Git, why it matters within hooks, and how to use it correctly with clear, copy-ready examples.

Hooks Overview

Hooks is something you will reach for often in Git. It keeps your workflow predictable and makes your intent obvious to reviewers and teammates.

When you learn hooks properly, you avoid the guesswork that leads to mistakes and rework. The example below shows the shape you will use in most real Git projects.

# .husky/pre-commit
npx lint-staged

# package.json
{
  "lint-staged": {
    "*.js": ["eslint --fix", "prettier --write"]
  }
}

Git hooks (via Husky) run checks like linting automatically before a commit is created.

Hooks Example

git add <files>
git commit -m "message"
git push origin <branch>
  • Start from a minimal Hooks example and grow it only as needed.
  • Keep things explicit so Hooks behaves the same for everyone on the team.
  • Name things clearly so teammates understand your Hooks at a glance.
  • Verify Hooks works as expected before relying on it in important work.

Git Cheatsheet

Handy Git command reference related to hooks.

Task Command Purpose
Status git status See what changed
Stage git add <file> Prepare changes to commit
Commit git commit -m "msg" Record a snapshot
Branch git switch -c name Create and switch branch
Merge git merge branch Combine histories
Push git push origin main Share commits
Pull git pull origin main Get others' commits
Undo git restore <file> Discard local changes

How Hooks Works in Git

Hooks builds on Git's model of snapshots and references. Every commit points to a full snapshot of your project, and branches are simply lightweight pointers to commits.

Git hooks (via Husky) run checks like linting automatically before a commit is created.

  • The working directory, staging area, and repository are Git's three main areas.
  • Commits are immutable snapshots linked to their parents.
  • Branches and tags are just pointers to commits.
  • Remotes are copies of the repository you sync with.

Practical Guidance for Hooks

On real teams, hooks works best with small, focused commits and clear messages. Commit often, write descriptive messages, and pull before you push to avoid surprises.

Habit Why it helps
Small commits Easier to review and revert
Clear messages History explains the why
Branch per feature Isolates work in progress
Pull before push Avoids avoidable conflicts

Common Mistakes

  • Copying hooks commands or snippets without understanding what each part does.
  • Skipping edge cases and error handling when using hooks.
  • Not verifying the result of hooks before moving on.
  • Over-complicating hooks before you actually need the extra flexibility.

Key Takeaways

  • Hooks is a core part of working effectively with Git.
  • Start small and keep hooks focused on a single goal.
  • Apply consistent patterns so hooks scales across your project.
  • Practise and document hooks to keep your workflow maintainable.

Pro Tip

Practise hooks on a throwaway project first. It is far cheaper to learn Git mistakes there than on important work.