0

I have created a custom Git Hook for "commit-msg" which says I should add a specific text for the commit message, I want this hook to be a part of my git repo centrally so that whoever clones the repo should get this hook in his local. enter image description here I am not able to push this hook as part of the git add/commit/push command, could anyone let me know if it possible to add hook? how to push git hook to remote repo?

Thanks, Abhishek

phd
  • 82,685
  • 13
  • 120
  • 165
Abhishek Anvekar
  • 338
  • 1
  • 5
  • 18

1 Answers1

2

TL;DR: Hooks are not part of the repository and can therefore not be pushed/pulled

As you may know, there is a difference between client-side and server-dide hooks.

Client side hooks are run on the client and configured on each client.

If you would be able to push them, you would have a security vulnerability in git as the each client would just when doing git actions.

You can instead save the hooks somewhere in your repository (checked in) and create a script that copies them to the .git/hooks directory.

You can then tell the contributers to run that script when cloning the repository in the contributing guidelines or the README.

You can also tell your users to run the following command in the repository in order to execute hooks in the directory you want:

git config core.hooksPath <your hooks directory>

If you want to create a server-side hook, you will need to upload the hook to the server manually.

Servers like GitHub do not allow to create custom server-side hooks but can configure things like protected branches.

dan1st
  • 12,568
  • 8
  • 34
  • 67