Setting up Git Commit Email Notification
Tags: git, server config, shell, tutorials
A method to send email notification to a list email addresses by the remote git server after every push from the client will be introduced.
After the client pushing to the git server, several actions are triggered. These actions are done by hooks. Hooks are scripts placed in $GIT_DIR/hooks directory. One of the trigger action is post-receive, so we can put a executable script with name post-receive under $GIT_DIR/hooks. For every push by client, post-receive will be executed to send email notification.
A sample notification email after client pushing two commits is show in the graph on the right. The subject contains a prefix, the repository name and the last commit message. The body of the email contains the commit log and summary of the changes.
Now lets go to the detail of configuring the mechanism.
Get the post-receive script
My script that sends email like the example described above can be downloaded from here:
http://github.com/zma/usefulscripts/blob/master/script/post-receive
This script use smtp server to send email. You need to change the smtp server address at the end of the script. Simply change smtp=”smtp://smtp.cse.ust.hk” to the smtp server you use. You can not use my smtp server which is only allowed to send email without authorization inside of CSD in HKUST. If you like to use Gmail’s smtp, please refer to Sending Email from mailx Command on Linux Using Gmail’s Smtp.
This script is changed from Andy Parkins’s one which is in git’s distribution. On fedora 12, the original script can be found under /usr/share/git-core/contrib/hooks.
Put post-receive into hooks directory
After getting the post-receive script, you need to copy it to $GIT_DIR/hooks/ directory on the git server. $GIT_DIR means the repository root directory here.
Please note that this script should be executable. You may need to add executable mod bits to post-receive by
chmod a+x hooks/post-receive
when you are in $GIT_DIR.
Change repository description
The repository name in the email subject is the first line of $GIT_DIR/description. Change the first line of the description file to you project’s name.
Change config
The sender’s email address, the mail list and the subject prefix is defined in $GIT_DIR/config file. I like to edit this config file by hand which is as easy as using git config command.
Add these line to the config file:
[hooks] mailinglist = "email1@example.com email2@example.com" senderemail = "owner@exmaple.com"
The email addressed in mailing list is separated by space.
The default subject prefix is [GIT]. You can change it to any string you like by add hooks.emailprefix in config.
Now the email notification mechanism has been set up. You can change the script if more functions is needed.
Updated on Apr. 20, 2010. change “postreceive” to “post-receive” in “chmod” part.
Read more:
- Sending Email from mailx Command in Linux Using Gmail’s Smtp
- Setting up POP3/IMAP Email Box on Nokia E71
- Setting up Git Server using Gitosis
- Setting up Stable Xen Dom0 with Fedora: Xen 3.4.2 with Xenified Linux Kernel 2.6.32.13 in Fedora 12
- Setting up Stable Xen Dom0 with Fedora: Xen 3.4.3 with Xenified Linux Kernel 2.6.31.12 in Fedora 12
- Setting up a NFS Server on Top of tmpfs /dev/shm

















Hi, cool script thanks!
Doesn’t work for me though, failing with the following issue:
git stuff
……
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
cat: /tmp/git-email-subject-10489: No such file or directory
mailx: invalid option -- 'S'
usage: mailx [-eIinv] [-a header] [-b bcc-addr] [-c cc-addr] [-s subject] to-addr …
[-- sendmail-options ...]
mailx [-eIiNnv] -f [name]
mailx [-eIiNnv] [-u user]
I hate bash, can’t read it, sorry. otherwise i’d go hunting for this bug myself!
@Rainer
It seems that you are using a distribution with old (not too old) software. In RHEL/CentOS 5, the mailx version are very old and it doesn’t support -S option. You can try to put these parameters into the ~/.mailrc file.
aaah, thanks for the quick reply!
i managed to find the -S option and took it out. had to also remove -r and now i’m happily sending mails, thanks!
:)
still have ugly output, though, something is wrong around the msg_count variable:
Counting objects: 5, done.
Delta compression using up to 2 threads.
Total 3 (delta 2), reused 0 (delta 0)
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
hooks/post-receive: 706: msg_count+=1: not found
[: 706: ==: unexpected operator
cat: /tmp/git-email-subject-10682: No such file or directory
…
but things work! thanks!
Hi Rainer,
You can find out the lines that have the msg_count and comment them out to bypass these warnings (there are only several lines). It seems the bash on your computer doesn’t support the ((msg_count+=1)) operation.
Nice to know that you find the script useful ;)