Getting your git repository to send out e-mails upon receiving updates

Here’s a useful tip for those who wants to be updated everytime someone pushes code into a shared git repository.

There’s a hook that comes with git that sends out an email to a defined e-mail address with information about a commit that has recently been pushed. It tracks the author, the commit message, the commit log as well as displays the changes made.

Lets get started.

Prepare your remote git repository

In your remote server where your remote git repository resides, you should find the following file in remote_repository_path/.git/hooks

post-receive.sample

#!/bin/sh 
# An example hook script for the "post-receive" event. 
# "post-receive" script is run after receive-pack has accepted a pack
# and the repository has been updated.  It is passed arguments in through
# stdin in the form
# For example:
#  aa453216d1b3e49e7f6f98441fa56946ddcd6a20 68f7abf4e6f922807889f52bc043ecd31b79f814 refs/heads/master
# see contrib/hooks/ for an sample, or uncomment the next line and
# rename the file to "post-receive"
#. /usr/share/doc/git-core/contrib/hooks/post-receive-email 

If there is a post-receive.sample file

Rename post-receive.sample to post-receive and give it executable rights.

/remote_repository_path/.git/hooks  $ mv post-receive.sample post-receive
/remote_repository_path/.git/hooks  $ chmod +x post-receive

In the new post-receive, uncomment the line that says

. /usr/share/doc/git-core/contrib/hooks/post-receive-email 

If there ISNT a post-receive.sample file

Copy and paste the above post-receive.sample snippet into remote_repository_path/.git/hooks/post-receive.

Remember to give it chmod +x rights.

Configure your post-receive-email hook

Still in your remote_server, check that you have a file at /user/share/doc/git-core/contrib/hooks/post-receive-email

If you don’t, copy and paste the content from here into the path above.

Configure the mailing address

Once that done, all you need is to configure the repository to send the email to the correct address. Still in your remote repository, edit the .git/config file and add the following:

[hooks]
mailinglist = whatever@email_address_here
showrev = "git show -C %s; echo"
envelopesender = [email protected]
emailprefix = "[GIT] "

This will ensure that the full git diff will be embedded in the e-mail.

Do refer to the post-receive-email file for more available config hooks

Enjoy!

Now in your local repository, make your changes and push it up. You should receive an e-mail in your configured e-mail address.

Extra Tip

To get rid of the ‘UNNAMED PROJECT’ line appearing in your email subject line, in your remote repository, edit .git/description with whatever you want to appear.