git post-receive Hook
At my job we use git for version control, and I wanted to do a few things whenever we pushed changes to the main tree. It took me a little bit of effort to figure out how it works, and below are my notes.
The post-receive hook is called when the repository receives changes. Let’s assume I have a git repository on my development machine that is a clone of a public git repository on a server. Then, when I run git push in my local repository in order to push my commits to the public repository, the post-receive hook is called on the public repository on the server.
Email Notification
On Debian-based systems there is an example email notification post-receive hook in /usr/share/doc/git-core/contrib/hooks/post-receive-email. It’s also available online from the git repo on github in the contrib/hooks directory.
In the repository that will be receiving commits (the public repository on the server, in the example above), first we need to make the post-receive hook executable:
$ cd /path/to/git/repo/.git/hooks $ chmod +x post-receive
Then we need to call the post-receive-email script from inside this post-receive hook. Open .git/hooks/post-receive with your favorite editor and make sure this line exists:
. /path/to/post-receive-email |
Notice we’re not executing the post-receive-email script, we’re sourcing it.
Next, we need to tell git where to send the email to. The hooks.mailinglist property controls this:
$ cd /path/to/git/repo $ git config hooks.mailinglist "mailinglist@example.com"
At this stage emails will be sent each time changes are pushed to the public git repository containing a summary of the new changes.
By default, emails refer to the project as “UNNAMED PROJECT”. To change this, edit .git/description.
For further customization options, look inside post-receive-email. For example:
$ cd /path/to/git/repo # envelopesender? see `man sendmail` for the -f switch $ git config hooks.envelopesender "git@example.com" $ git config hooks.emailprefix "[MY PREFIX] " # note the trailing space
Kicking Off a Buildbot Build
You may also want to have Buildbot start a build from the post-receive hook. Like the email script, the Buildbot git script expects revision hashes on standard input. The post-receive script then needs to be modified a little to pass the hashes it receives via standard input to all the scripts it invokes. Here’s a new copy of post-receive:
while read oldrev newrev refname do echo $oldrev $newrev $refname | /bin/sh /path/to/post-receive-email echo $oldrev $newrev $refname | /usr/bin/python /path/to/git_buildbot.py done |
This script first reads oldrev, newrev, and refname from standard input, then pipes those values into post-receive-email and git_buildbot.py. We loop until there is no more input, presumably because another push may happen while we’re executing.
hi,
thank you for this information.
in this you gave how to get notification when a push is made.
how to make to get notifications when a push is made a particular branch in the repository.
i need to get notification for group of people and when push is made by a developer to a branch on the repository.
can we make it with post receive?
@pavan you will probably have to write some code to perform the check you want for you. This blog post might help: http://gitready.com/intermediate/2009/04/03/find-ancestor-commits.html
Good luck!
Thank you! Thank you! Thank you!
I couldn’t figure out why my post-receive-email was not working in my while loop.
This really helped:
echo $oldrev $newrev $refname | /bin/sh /path/to/post-receive-email
@Emily — glad it helped