6

I have written simple shell script to throw "success and failure message" and placed it under .git/hooks/ with all proper permissions. I want to call this script as a post-receive. But the script is not working, running the script simply works but as post-receive hook it doesn't work.

Is their something being missed or have i wrongly understood the post-receive hook. Can some one explain client-side and server-side hooks and how to execute them.

I have searched over it but not able to understand.

2
  • I'd suggest showing us a little more, like the script you are using and how you are trying to invoke it. Also, the Git Hooks section of Pro Git is helpful, as well as the githooks man page. Jan 23, 2015 at 10:23
  • Did you perhaps forget to mark it executable? An explicit sh ./post-receive will work regardless, but almost nothing else (and not this).
    – jthill
    Jan 23, 2015 at 15:07

2 Answers 2

12

To enable the post-receive hook script, put a file in the hooks subdirectory of your .git directory that is same named (without any extension) and make it executable:

touch GIT_PATH/hooks/post-receive
chmod u+x GIT_PATH/hooks/post-receive

For more info check this doc: https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks

Example

Check this example (a simple deploy) GIT_PATH/hooks/post-receive:

#!/bin/bash
TARGET="/home/webuser/deploy-folder"
GIT_DIR="/home/webuser/www.git"
BRANCH="master"

while read oldrev newrev ref
do
    # only checking out the master (or whatever branch you would like to deploy)
    if [[ $ref = refs/heads/$BRANCH ]];
    then
        echo "Ref $ref received. Deploying ${BRANCH} branch to production..."
        git --work-tree=$TARGET --git-dir=$GIT_DIR checkout -f
    else
        echo "Ref $ref received. Doing nothing: only the ${BRANCH} branch may be deployed on this server."
    fi
done

Source: https://gist.github.com/noelboss/3fe13927025b89757f8fb12e9066f2fa#file-

1
  • I did not understand what is the difference between TARGET and GIT_DIR... Jul 26, 2022 at 12:54
2

It needs to be called post-receive (no extension, no post-receive.sh for instance).

If it is placed (as the OP did) in .git/hooks folder, and made executable, it will be called when you are pushing to that repo (since it is a server hook).
If you were to install it on your own local repo, it would not be called (unless you somehow push to your own repo, which seems unlikely).

For a remote Git hosting server like GitHub, you would need to implement that hook as a webhook (a listener to GitHub push event).

6
  • How do I install the hook "on server" ? I have just placed it in the .git/hooks folder and made it executable. is there something else that needs to be done?
    – Sumit
    Jun 18, 2018 at 19:32
  • @Sumit what git remote -v returns? What is your server?
    – VonC
    Jun 18, 2018 at 19:33
  • that returns the following: origin github.<corp>.com/sumit/webhooks.git (fetch) origin github.<corp>.com/sumit/webhooks.git (push) where <corp> is my org name.
    – Sumit
    Jun 18, 2018 at 20:22
  • @Sumit If this is managed by GitHub (meaning it is not on premise, managed by your company), then you cannot write a post-receive hook, since it is a server-side hook, as described in git-scm.com/book/en/v2/…. You might have a look to developer.github.com/webhooks.
    – VonC
    Jun 18, 2018 at 20:24
  • oh I think I understood. Webhooks is probably what I need. Thanks for pointing me in the right direction!
    – Sumit
    Jun 18, 2018 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.