Improve Git Emails

This is just a quick tip on how to hack gitosis to allow you to have better subject lines in your emails. This assumes you are using gitosis, not gitolite which many people hosting their own git repositories are moving to because it supports per branch and granulated user permissions. If you are looking on a full article on how to install gitosis check out this post.

So grab gitosis

git clone git://

Apply this patch to set an environment variable for the user that is doing a push

diff --git a/gitosis/ b/gitosis/
index d83b1d8..a6a49a2 100644
--- a/gitosis/
+++ b/gitosis/
@@ -200,6 +200,7 @@ class Main(app.App):
         main_log.debug('Serving %s', newcmd)
+        os.putenv('USER', user)
         os.execvp('git', ['git', 'shell', '-c', newcmd])
         main_log.error('Cannot execute git-shell.')

Then install gitosis with that change

$ sudo ./ install

Now, edit the /usr/local/bin/git-post-receive-email script to change the subject line to make more sense

--- /usr/local/bin/git-post-receive-email.old	2010-02-06 19:29:35.000000000 +0000
+++ /usr/local/bin/git-post-receive-email	2010-02-06 19:14:03.000000000 +0000
@@ -186,7 +186,7 @@
 	# Generate header
 	cat <<-EOF
 	To: $recipients
-	Subject: ${emailprefix} $refname_type, $short_refname, ${change_type}d. $describe
+	Subject: ${emailprefix} push from ${USER}- $refname_type, $short_refname, ${change_type}d.
 	X-Git-Refname: $refname
 	X-Git-Reftype: $refname_type
 	X-Git-Oldrev: $oldrev

If you haven’t setup email already make sure that you have the hook installed by making a symbolic link in /home/git/repositories/myrepo.git/hooks

ln -s /usr/local/bin/git-post-receive-email post-receive

And now your subject changes from this

[GIT] branch, master, updated. d61c9b93446a13093ef7f510a588c69a606ff762

to this

[GIT] push from jgoulah- branch, master, updated.

Now we know who is pushing code by glancing at the subject line. The username comes from the filenames in gitosis-admin/keydir/. Obviously you could style the subject line however you see fit.

Git  gitosis