Git hooks are similar to SVN hooks; you can execute a script at a point in the Git routine. Git hooks are both local and server-side. The local hooks pertain to local activities, like committing, merging, checking out, etc. The server-side hooks deal with receiving pushes from a local client.
The scripts that Git hooks execute are stored in the
.git/hooks
directory of your project. In every repository there are a collection of sample hooks that you can rename and use as inspiration for your own.
The scripts can be in any language that support executable scripts. The samples are mostly shell scripts, but you can use Perl, Ruby, Python, or something else that is familiar to you.
Here are all the hooks available in Git:
pre-commit
prepare-commit-msg
commit-msg
post-commit
applypatch-msg
pre-applypatch
post-applypath
pre-rebase
post-rewrite
post-checkout
post-merge
pre-push
pre-receive
update
post-receive
Local Git Hooks
Local Git hooks reside in the
.git/hooks
directory and are not treated as project files. They are not tracked in the Index and because of that they are not included when someone clones a repository.
Because of this there are some limitations to keep in mind while working with Git hooks. First and foremost: local hooks are not a reliable way to enforce policies, like a commit message structure. And, because you are unable to include the contents of the
.git/hooks
directory in version control, you will need to consider a way to share hooks that you’d like your team to use.
Here are the local hooks:
pre-commit
prepare-commit-msg
commit-msg
post-commit
applypatch-msg
pre-applypatch
post-applypath
pre-rebase
post-rewrite
post-checkout
post-merge
pre-push
Server-side Hooks
Server-side hooks are set up to kick off scripts when a typical remote repository actions takes place.
There are only three server-side hooks:
pre-receive
update
post-receive
They all operate around when the server receives a
git-push
from a client. These are reliable methods for enforcing some sort of repository or standard. You can run checks, kick of an integration script, testing, etc.Implement a Git Hook
For our example we’re going to implement a local hook. We’d like to display a message after each commit, reminding us to push our commits to the remote repository.
The setup for this message will be to use the
post-commit
hook. This hook is triggered after each successful commit. For our implementation, we want the hook to trigger and display a message for us.
There isn’t a sample file for post-commit
already in place. That’s okay, we can create it.
Create a new file called
post-commit
in .git/hooks
.
I’m going to use Ruby as the scripting language. Remember, Git Hooks can use any executable script. But I like Ruby so we’ll use that. This Ruby code is as simple as it comes, with just a single
puts
statement.
Add this code to the
post-commit
file:#!/usr/local/bin
puts "======================="
puts "Please remember to push your commits!"
puts "======================="
With the code in place, we can then save the hook file and set it as executable.
$ chmod +x post-commit
Test it by making a change to our project and creating a commit. If you see the message, it worked!
If you didn’t, check your code, that the hook file is executable, and that you have it properly named.
In another article I’ll talk about triggering hooks on the server.
0 comments:
Post a Comment