Post Commit Hook Not Running
Asked Answered
N

4

55

My post commit hook is not running after git. I have verified that the hook does work if I just run it from the terminal. The code in the hook is:

#!/bin/sh
#.git/hooks/post-commit
# An example hook script that is called after a successful
# commit is made.
#
# To enable this hook, rename this file to "post-commit".

perl -pi -e 's/([a-f0-9]+)$/'$( git rev-parse HEAD )/ ../../config/commit.git

I did rename the file to post-commit in ./.git/hooks/ and the permissions are -rwxr-x-r-x so I am not sure why it doesn't work.

Northumbria answered 22/2, 2011 at 21:21 Comment(0)
L
31

Try putting some echo lines before and after the perl line like this:

echo "post-commit started"
perl ...........
echo "post-commit finished"

This way you can confirm if the script is actually running, because when you run

git commit

you should see

post-commit started
post-commit finished

Towards the end of your output.

Loy answered 22/2, 2011 at 21:33 Comment(2)
thanks. The post-commit was running, but it did have an error. The path should have been ./ rather than ../../.Northumbria
Steven Lu's permissions answer bit should be added to this accepted answer.Pelfrey
B
214

I'll leave this here as an answer because I stumbled upon my own answer for when my post-commit hook wasn't running:

chmod +x .git/hooks/post-commit

Probably applies to any kind of hook. In fact, probably applies to any kind of script.

Bourbonism answered 8/1, 2013 at 5:40 Comment(2)
what if you have permissions 777 and hook still not running? post-checkout runs , but post-receive not, when it is exactly same commands. It should run on git pull.Nonstriated
@Darius.V maybe it needs to actually receive something rather than "Already up-to-date."? Just a thought.Bourbonism
L
31

Try putting some echo lines before and after the perl line like this:

echo "post-commit started"
perl ...........
echo "post-commit finished"

This way you can confirm if the script is actually running, because when you run

git commit

you should see

post-commit started
post-commit finished

Towards the end of your output.

Loy answered 22/2, 2011 at 21:33 Comment(2)
thanks. The post-commit was running, but it did have an error. The path should have been ./ rather than ../../.Northumbria
Steven Lu's permissions answer bit should be added to this accepted answer.Pelfrey
E
27

My post-commit script wasn't being called because:

I had named the script post-commit.sh, rather than just post-commit.

To enable a hook script, put a file in the hooks subdirectory of your .git directory that is named appropriately (without any extension) and is executable. From that point forward, it should be called. We’ll cover most of the major hook filenames here. See git-scm

Not sure why I had in my head that hooks needed the bash file extension.

I also didn't realize hook scripts cannot have file extensions. For example,

If you want to use the bundled hook scripts, you’ll have to rename them; their file names all end with .sample

Hope this helps someone.

Eakin answered 7/12, 2016 at 20:57 Comment(2)
I also missed that part of the instructions; I just saw but any properly named executable scripts will work fine and dropped in pre-commit.sh. Ended up on this question, saw the most popular answer, and thought "yeah, I already did that". Then I noticed your answer. Thanks!Preterhuman
This is a good answer! As far as I can tell in UNIX-land, file extensions aren't much of a thing. It's simply a convention that Windows has embraced. Much of the confusion related to this could be traced to Windows' default behavior of hiding those extensions, which, even on Windows, are very much a crucial portion of the file's name and path.Bourbonism
M
1

In addition to the answers noted here, note that if you are expecting user input in your hook, you need to redirect standard input to the keyboard like so (at least for a bash script);

exec < /dev/tty
Mahogany answered 24/9, 2019 at 19:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.