Get user and commit details in git pre-receive hooks


I am new to git and git-hooks. I am tryig to validate every push that is made and for that I need two things:-

  1. User’s details (email-id and name) who made the push
  2. Commit messages of all the commits in that push

I need to do that using Git’s pre-receive hook, but I am not able to any of the above two mentioned things.

I have been searching this issue on SO, githooks’s doc and other sites but nothing worked for me.

Thanks in advance.


There is a gitlab tag in the question, so I assume you’ve read Gitlab Server Hooks.

From the githooks doc on pre-receive, we can see that the hook doesn’t take any parameter and it reads ref info from standard input. To read from standard input in bash, you can refer to the pre-push sample, which you can also find in the local repository’s .git/hooks/pre-push.sample if you haven’t specified init.templateDir in gitconfig. pre-receive uses a different line format from pre-push, so modify the while read part,


# <old-value> SP <new-value> SP <ref-name> LF
while read oldv newv ref;do
    # get new commits
    newcommits=$(git rev-list ${oldv}..${newv})

    # get committer and email and message of each commit
    for commit in ${newcommits};do
        committer=$(git log -1 ${commit} --pretty="%cn")
        email=$(git log -1 ${commit} --pretty="%ce")
        message=$(git log -1 ${commit} --pretty="%B")

The above sample misses some checks like if it’s a ref creation or deletion like in the pre-push sample, and if there’s any new commit. It’s recommended to add these checks. The hook does not know the pusher’s Gitlab account. The committer name and email may not accord with the account.

As git push can send one or more given strings to the server by one or more -o <string> or --push-options=<string> and Gitlab supports the feature, you could also add codes to receive these strings. You can find how to do it in the pre-receive sample.

Answered By – ElpieKay

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published