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.

Aniruddha Joshi
  • 113
  • 1
  • 2
  • 4
  • 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](http://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks) section of Pro Git is helpful, as well as the [githooks](http://git-scm.com/docs/githooks) man page. – John Szakmeister Jan 23 '15 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 '15 at 15:07

2 Answers2

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-

Isius
  • 6,712
  • 2
  • 22
  • 31
fitorec
  • 4,257
  • 2
  • 24
  • 18
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).

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 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 '18 at 19:32
  • @Sumit what `git remote -v` returns? What is your server? – VonC Jun 18 '18 at 19:33
  • that returns the following: origin https://github..com/sumit/webhooks.git (fetch) origin https://github..com/sumit/webhooks.git (push) where is my org name. – Sumit Jun 18 '18 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 http://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks#Server-Side-Hooks. You might have a look to https://developer.github.com/webhooks/. – VonC Jun 18 '18 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 '18 at 20:30
  • I have a similar question [here](https://stackoverflow.com/questions/64759535/automatically-merge-one-branch-into-another-basis-on-some-conditions-using-hook) where I am trying to use git hook to merge branch. I wanted to see if you can help me out. – AndyP Nov 10 '20 at 22:53