A method to send email notification to a list of email addresses by the remote git server after every push from the client will be introduced in this post. An example notification email after a commit is shown in the figure below. The subject contains a prefix, the repository name, the branch name and the last commit message. The body of the email contains a summary of the changes and the commit log.
How does it work
Table of Contents
After the client pushing to the git server, several actions are triggered. These actions are done by hooks. Hooks are scripts placed in the GIT_DIR/hooks directory. Here, GIT_DIR is the directory of the git repository on the git server. 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 clients, post-receive will be executed to send email notification.
Now lets go to the detail of how to set up the email notification.
The script uses
mailx to send emails out. You need to install
mailx on your git server.
On Fedora 12+/RHEL6+/CentOS6+, the
mailx package works just fine.
On Debian/Ubuntu/Linux Mint, please install the
heirloom-mailx package instead of using the default
bsd-mailx package. The mailx from
bsd-mailx does not support all the options used such as
-r in the script.
Get the post-receive script
My script that sends email like the example described above can be downloaded from: git post-receive script.
Or view the source code: git post-receive script
This script uses
mailx to send email by default which uses the local message transfer agent service (like
sendmail) to send email. You need to enable the message transfer agent to send out emails on your git server. You can also choose to use smtp to send out email, just find the line
# cat $email_tmp_file | mailx -S smtp="smtp://smtp.cse.ust.hk" -s "$emailsubject" -r $senderemail $recipients
near to the bottom of the script, uncomment it out, comment out the next line which calls
mailx, and change the smtp server to yours. The script will use smtp server to send email. If you like to use Gmail’s smtp, please refer to
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 the post-receive script into hooks directory
After getting the post-receive script, you need to copy it to or make a soft link (which I do since there usually many repositories) in the GIT_DIR/hooks/ directory on the git server.
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.
If you use soft links, setting the original post-receive file once is sufficient.
Change the 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 the config file
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 = "email@example.com firstname.lastname@example.org"
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 adding
hooks.emailprefix in the config file.
Now the email notification mechanism has been set up. You can change the script if more functions are needed. If you think your new version is great and would like to share it, send me a pull request.