34

I have a bare repo server-side, and I am able to successfully commit and push from my local machine. However, the post-receive hook is not running. Details:

  • Using SSH as protocol
  • I have renamed the standard "post-receive.sample" to "post-receive"
  • This file has -rwxr-xr-x permissions
  • The file is owned by the same user that owns the repo, which is the same SSH user that logs in and pushes
  • The actual pushing goes fine; files are updated - it's just the hook that does not run
  • I tried putting echo "Some text" before and after the hook, but this is not shown (see: Post Commit Hook Not Running).
  • Hook script is included below, although this appears not to be causing the problem
  • Using git 1.7.0.4 on Ubuntu 10.04

.

user@server:/home/repos/project1/hooks# cat post-receive
#!/bin/sh
echo "Hook is running..."
export GIT_WORK_TREE=/home/web/project1/www/
git checkout -f
rm -rf /home/web/project1/www/temp/
Community
  • 1
  • 1

7 Answers7

62

In order for a Git hook to run, it needs to have permissions set to allow it to be executable. If a hook doesn't seem to be running, check the permissions, and make sure it's executable. If it isn't you can make all hooks executable like this:

chmod ug+x .git/hooks/*

...or if you want to make a single hook (eg. post-receive) executable:

chmod ug+x .git/hooks/post-receive

(Thanks to this post)

Nick F
  • 9,781
  • 7
  • 75
  • 90
32

I had this problem. I had a typo in my script filename.

post-recieve instead of post-receive

dericbytes
  • 461
  • 5
  • 5
  • 1
    just spent about an hour trying to debug this... your answer turned out to be correct ‍♂️ thank you – Bjarke Dec 11 '19 at 16:35
13

The issue was related to the mounting of the filesystem. The partition was mounted as noexec, and therefore no files could be executed. This caused the hook not to run. I removed the noexec flag and it now works just fine.

  • 3
    How did you remove this no hooks part? – Armeen Moon Dec 28 '14 at 04:48
  • 3
    I had the same issue. I moved the git dir to a different location (e.g. `mv .git "$HOME/repo.git"`) and then ran `git init --separate-git-dir=$HOME/repo.git` to initialize the directory again. After that, the hooks did run as expected (make sure that the `GIT_WORK_TREE` environment variable or other overrides are not set, otherwise the worktree from `.git/config` won't be used). – Rob W Apr 06 '15 at 13:42
  • Anyone found a workaround where we are not able to move the .git folder into an `exec` enabled partition? – a coder May 14 '18 at 18:02
  • My issue ended up being my script was silently failing due to permissions on the files I was trying to run, though it was working. I had to use su -l -c "blah" to test what was going on. – abelito May 05 '19 at 09:00
7

Seems GIT will NOT run the post-receive hook if there are no changes to the code base.

In my case,

The post hook was not getting executed, but the "push" operation kept returning the following message.

Everything up-to-date

So I just created an empty file in my code, did commit and then pushed to remote. On which the post-receive hook got executed.

Rakesh
  • 1,336
  • 2
  • 11
  • 13
  • Have the same issue but do not want even empty commits to clutter my branches. No other option? – trainoasis Aug 08 '19 at 10:39
  • 2
    I faced this issue when I was testing repeatedly in order to tweak my code deployment. Once the setup is completed, in the the real world, there will anyway be changes in the code base. so this issue wouldn't concern most of us. Having said that, the answer your question is, I have not investigated this further. so i don't know at the moment. – Rakesh Aug 19 '19 at 05:19
  • An alternative to this if your situation allows, is to delete the remote branch and re-push. – Kiee Dec 02 '19 at 09:40
  • To answer the follow-up question, you can force an update without modifying the stack by setting the remote repo to an earlier commit ie- git commit -f branch repo:commit_id ..I'm dealing with this right now and that's the best way I've found to do it. – Chris Sep 23 '20 at 18:03
4

I used MacBook and this helped in my situation:

chmod ug+x .husky/*
chmod ug+x .git/hooks/*
Roman
  • 19,236
  • 15
  • 93
  • 97
0

I had the same problem on a Centos 6 system, where it turned out that SELinux prevented hooks scripts from running. Turning httpd_git_script_t into a permissive domain helped here (since "sesearch -A -s httpd_git_script_t -p exec" yielded nothing, ie. no process running in the httpd_git_script_t domain was allowed exec permission):

semanage permissive -a httpd_git_script_t
micha137
  • 1,195
  • 8
  • 22
-3

Are you sure it's not running? It must be running, you just can't see it. My guess is that there is not stdout set to your ssh session at the time it's executed, so you won't ever see the output of your echo. The link suggests testing it locally, not via ssh.

Andrew Kolesnikov
  • 1,920
  • 1
  • 14
  • 20