Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?

1.6k Views Asked by At

Why all the script in my git hooks (pre-commit, post-commit, pre-receive, pre-push etc) do not run?

Note: this question is not a duplicate;

I have try the answer to each of the other questions but none of them work.

I did chmod +x, added the path to hook. rename script, neither of them solve my issue.

Inside my git

branches config description HEAD hooks info objects refs

Inside hooks:

applypatch-msg.sample  fsmonitor-watchman.sample  post-update.sample     pre-commit         prepare-commit-msg.sample  pre-rebase.sample  update.sample
commit-msg             post-merge.sh              pre-applypatch.sample  pre-commit.sample  pre-push                   pre-receive

I run them manually and they are all working fine.:

$ bash pre-commit
  You are about to commit to master
  Do you really want to do this? [y/n] y

pre-commit script

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git commit" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message if
# it wants to stop the commit.
#
# To enable this hook, rename this file to "pre-commit".

echo "You are about to commit" $(git diff --cached --name-only --diff-filter=ACM)
echo "to" $(git branch --show-current)

while : ; do
    read -p "Do you really want to do this? [y/n] " RESPONSE < /dev/tty
    case "${RESPONSE}" in
        [Yy]* ) exit 0; break;;
        [Nn]* ) exit 1;;
    esac
done

But when i git commit and git push to the repository none of the scripts work.

$git commit -m "Test hooks"
[master] Test hooks  1 file
changed, 1 insertion(+)

My git version is 2.39.1

I created the repository on a VM with Ubuntu 18.04.6 LTS installed Here was the procedure fro creating the repo.

mkdir project1.git
cd project1.git
git init --bare

After the creation i clone the repo to my local computer (windows).

Clone the git repository

git clone git@{ip}:/home/git/git_repositories/project1.git/

1

There are 1 best solutions below

5
On

But I want use the scripts in project1.git/hooks to make this work.

A pre-commit hook for instance would not work in a bare repository (which has no working tree).
It would work only in your cloned repository, on Windows, where you can create a myClonedRepo/.git/hook/pre-commit script (no extension, no .sh), which will be run before each commit.

From the comments:

  • all users could create/clone their repositories from a shared Git template repository. The article "Creating a Custom Git Template" gives an illustration of that approach, but means that:
    • every user must access the same shared folder
    • they need to activate the init.templateDir config setting in their global Git configuration
  • any check which must be enforced for all the team members, especially for a distributed team, is best managed by server-side hooks instead.