Git Hooks are scripts that run automatically before or after certain Git commands. They are a powerful tool that can help you automate tasks, enforce coding standards, and improve your workflow. This article will introduce you to Git Hooks and show you how to use them to enhance your development process.
What are Git Hooks?
Git Hooks are scripts that Git runs before or after certain commands. They are stored in the .git/hooks
directory of your Git repository and are executed automatically when you run a Git command. There are several types of Git Hooks, each corresponding to a different command or event in the Git workflow.
Pre-commit hooks, for example, run before you commit changes to your repository. They can be used to enforce coding standards, run tests, or perform other tasks to ensure the quality of your code. Post-commit hooks, on the other hand, run after you commit changes and can be used to send notifications, update issue trackers, or perform other tasks.
These hook scripts are only limited by your imagination and can be used to automate almost any task you can think of. They are a powerful tool that can help you improve your workflow, enforce best practices, and save time.
How to Use Git Hooks
To use Git Hooks, you need to create a script and save it in the .git/hooks
directory of your Git repository. The script should be named after the hook you want to use (e.g., pre-commit
, post-commit
, etc.) and should be executable and should have appropriate permissions (e.g., chmod +x pre-commit
).
Here is a full list of the available Git Hooks:
-
applypatch-msg
: Executes before a patch is applied, to edit the patch's commit message. -
pre-applypatch
: Runs before a patch is applied, useful for verifying patch integrity. -
post-applypatch
: Executes after a patch is applied, typically used for notifications. -
pre-commit
: Runs before the commit process starts, often used for linting or tests. -
prepare-commit-msg
: Runs before the commit message editor is fired up, to customize the message. -
commit-msg
: Runs after the commit message is created, for additional checks on the commit message. -
post-commit
: Executes after a commit is completed, often used for notifications or logging. -
pre-rebase
: Runs before the rebase process begins, useful for verifying the rebase can proceed. -
post-checkout
: Runs after a checkout is performed, typically used to configure the working directory. -
post-merge
: Runs after a merge is completed, commonly used for notifications or cleanup. -
pre-receive
: Runs before a push to the server is processed, used to enforce policies. -
update
: Runs during a push to update references, often used to enforce branch-specific policies. -
post-receive
: Executes after a push to the server is processed, for notifications or deployment. -
post-update
: Runs after references are updated on the server, useful for repository maintenance tasks. -
pre-auto-gc
: Runs before automatic garbage collection, often used to prevent unwanted GC. -
post-rewrite
: Runs after commands like git commit --amend or git rebase, for adjusting changes. -
pre-push
: Executes before a push to a remote repository, typically used to verify the push contents.
Note that all these hooks, if present by default, are stored as .sample
files in the .git/hooks
directory. You have to remove the .sample
extension to activate them and make them executable.
You can read more about these hooks here and here.
Example: Pre-commit Hook
Here is an example of a simple pre-commit hook that checks for whitespace errors in your code:
Another example of a pre-commit hook is mentioned below. This hook validates the git config’s global user email and checks whether a gpg key exists. The hook is useful so that the commits contain the correct committer email address and also to ensure the commits are signed.
Remember to make the script executable by running chmod +x pre-commit
and save it in the .git/hooks
directory of your Git repository.
Here's what it looks like:
Here, the pre-commit hook tries to check if the user's email address is set to a specific value. If the email address is not set to the expected value, the commit is aborted. In my case, the expected email address is work@domain.com
but I am logged in with a different email address. So, the commit is aborted.
Scripting languages
You can write Git Hooks in any scripting language you are comfortable with, such as Bash, Python, Ruby, or Perl. The only requirement is that the script should be executable and should have appropriate permissions.
Example of a prepare-commit-msg
hook written in Python:
Example: Post-commit Hook
Here is an example of a simple post-commit hook written in Python that sends an email notification after a commit:
This probably isn't the best way to send email notifications from a Git Hook, but it gives you an idea of what's possible.
Projects Using Git Hooks
Here are a few projects that use Git Hooks to automate tasks and improve workflow that are also mentioned on githooks.com:
-
Lolcommits - Takes a snapshot with your webcam every time you git commit code, and archives a lolcat style image with it.
-
Husky - Git hooks for Node.js, manage your hooks from your package.json.
-
podmena - Enhance your commit messages by adding random emoji.
-
overcommit - A well-maintained, up-to-date, flexible Git hook manager.
More Resources
Conclusion
Git Hooks are a powerful tool that can help you automate tasks, enforce coding standards, and improve your workflow. By using Git Hooks, you can save time, improve code quality, and ensure that your team follows best practices. I hope this article has given you a good introduction to Git Hooks and inspired you to start using them in your projects.