Git Hooks

Git hooks are scripts that Git executes before or after events such as: commit, push, and receive. Git hooks are a built-in feature - no need to download anything!


What are git hooks?

Git hooks are scripts that are run by Git before or after events such as: commit, push, and receive. Git hooks are run locally.

These hook scripts are only limited by a developer's imagination. Some example hook scripts include:

  • pre-commit: Check the commit message for spelling errors.
  • pre-commit: Email/SMS team members of a new commit.
  • post-receive: Push the code to production.

See a full list of possible hooks →


How do they work?

Every git repository has a .git/hooks folder with a script for each hook you can bind to. You're free to change or update these scripts as necessary, and git will execute them when those events occur.


What hooks are available?

Here's a full list of hooks you can attach scripts to:

  • applypatch-msg
  • post-commit
  • post-update
  • pre-commit
  • update
  • commit-msg
  • post-receive
  • pre-applypatch
  • pre-rebase

Why should I care?

Fair question! Git hooks can greatly increase your productivity as a developer. Being able to push to your staging or production environment without ever leaving git is just plain awesome. Update your code, make a commit and push, and you're code can be running in any environment you specify. No need to mess with ssh or ftp.


How do I implement git hooks?

The short and easy: Overwrite one of the scripts in .git/hooks and make it executable.


Reading


Contribute

If you have a git hook you love, or a resource you've written for the community - please get in touch with me.


Maintainer

Hi, my name is Matthew Hudson and I created this guide to help myself and others wrap our head around integrating git hooks with webhooks.