Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP C C++ C# AWS W3.CSS HOW TO BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR ANGULARJS GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SWIFT SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING INTRO TO HTML & CSS BASH RUST TOOLS

Git Tutorial

Git HOME Git Intro Git Install Git Config Git Get Started Git New Files Git Staging Git Commit Git Remove and Move Git Tagging Git Stash Git History Git Diff Git Blame Git Help Git Branch Git Merge Git Workflow Git Best Practices

Git and GitHub

GitHub Get Started Git What is SSH? GitHub Add SSH GitHub Set Remote GitHub Edit Code Pull from GitHub Push to GitHub GitHub Branch Pull Branch from GitHub Push Branch to GitHub GitHub Flow GitHub Pages Git GUI Clients

Git Contribute

GitHub Fork Git Clone from GitHub GitHub Send Pull Request

Git Undo

Git Revert Git Reset Git Clean Git Amend Git Rebase Git Reflog Git Recovery

Git Advanced

Git .gitignore Git .gitattributes Git Large File Storage (LFS) Git Signing Commits/Tags Git Cherrypick & Patch Git Bisect Git Merge Conflicts Git CI/CD Git Hooks Git Submodules Git Worktree Git Remote Advanced

Git and Bitbucket

Bitbucket Get Started Bitbucket Add SSH Bitbucket Set Remote Bitbucket Edit Code Bitbucket Branch Bitbucket Pages Bitbucket Fork Git Clone from Bitbucket Bitbucket Send Pull Request

Git and GitLab

GitLab Get Started GitLab Add SSH GitLab Set Remote GitLab Edit Code GitLab Branch GitLab Pages GitLab Fork Git Clone from GitLab GitLab Send Pull Request

Git Cert

Git Certificate

Git Reference

Git Glossary

Git Exercises

Git Terminal Git Exercises Git Challenges Git Quiz Git Syllabus Git Study Plan

Git Hooks


Share

What are Git Hooks?

Git hooks are scripts that run automatically when certain Git events happen, like making a commit or pushing code.


Why Use Hooks?

Hooks help you automate repetitive tasks, enforce coding standards, and catch problems early.

For example, you can:

  • Run tests before every commit or push
  • Check code style automatically
  • Block bad commit messages
  • Enforce rules for everyone on your team

Where Do Hooks Live?

Hooks are stored in .git/hooks inside your repository.

By default, you'll see sample scripts ending with .sample.

Example: List Available Hooks

ls .git/hooks
applypatch-msg.sample      pre-commit.sample          pre-receive.sample
commit-msg.sample          pre-merge-commit.sample    push-to-checkout.sample
fsmonitor-watchman.sample  prepare-commit-msg.sample  sendemail-validate.sample
post-update.sample         pre-push.sample            update.sample
pre-applypatch.sample      pre-rebase.sample
Try it Yourself »

How to Enable a Hook

To enable a hook, remove the .sample extension and make the script executable.

For example, to enable pre-commit:

Example: Enable pre-commit Hook (Linux/macOS)

mv .git/hooks/pre-commit.sample .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit

On Windows, also rename the file to exactly pre-commit, with no file extension. Git for Windows runs hook scripts that start with #!/bin/sh using its bundled shell, so the same script works on every system.



Types of Hooks

There are many types of hooks, but the most common are:

  • pre-commit
  • commit-msg
  • pre-push
  • pre-receive
  • post-receive

pre-commit Hook

The pre-commit hook runs before you make a commit.

You can use it to check code style, run tests, or stop a commit if something is wrong.

Example: Simple pre-commit Hook

#!/bin/sh
# Stop commit if any .js file has "console.log"
grep -r 'console.log' *.js && {
  echo "Remove console.log before committing!"
  exit 1
}

commit-msg Hook

The commit-msg hook checks or edits the commit message.

For example, it can block commits without a ticket number.

Example: commit-msg Hook

#!/bin/sh
# Block commit if message does not contain a ticket number
if ! grep -qE 'JIRA-[0-9]+' "$1"; then
  echo "Commit message must have a ticket number (e.g. JIRA-123)"
  exit 1
fi

pre-push Hook

The pre-push hook runs before you push code to a remote.

You can use it to run tests or checks before sharing code.

Example: pre-push Hook

#!/bin/sh
npm test || exit 1

Server-side Hooks

Some hooks (like pre-receive) run on the Git server, not your computer.

These can enforce rules for everyone who pushes to the repository.

Example: pre-receive Hook

#!/bin/sh
# Block pushes to the main branch
if grep -q ' refs/heads/main$'; then
  echo "Pushing to main is not allowed"
  exit 1
fi

Custom Hooks

You can write any custom script as a hook.

Just put it in .git/hooks and make it executable.

Example: Custom Hook

#!/bin/sh
echo "Hello from my custom hook!"

Debugging and Best Practices

  • Make sure your hook script is executable (chmod +x scriptname).
  • Add echo statements to see what your script is doing.
  • Check the exit code: exit 0 means success, exit 1 means fail.
  • Name hooks exactly (like pre-commit, with no file extension). Git for Windows runs #!/bin/sh scripts with its bundled shell.
  • Keep hooks simple and fast; slow hooks slow down your workflow.
  • Share useful hooks with your team (but remember: hooks are not versioned by default).

Note: Hooks are powerful for automating checks (like linting or tests) and enforcing team standards. Client-side hooks run on your computer. Server-side hooks run on the Git server.




Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookies and privacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.