How can I add my pre-commit hook to an existing git repository
checking it out my colleagues have it instantly in place
Sept. 2015: That is not possible: a hook can be put in source control (you simply copy the script in your git repo), but it cannot be "automatically in place" (active) on clone/checkout: that would be way too dangerous, depending on what the hook is actually doing.
See also "Git remote/shared pre-commit hook"
You would still need to activate it, by adding a symlink (even on Windows) to the pre-commit hook present in the git repo.
Update Dec. 2016: the OP Calamity Jane mentions in the comments:
I solved it by now in symfony2 projects (and with others, it also should work) to have it as part of the
composer.json
.
So if a colleague is doing acomposer install
orcomposer update
, it is automatically placed in the correct place.
"scripts": { "dev-install": [ "bash setup_phpcs.sh" ] },
So on dev,
setup_phpcs.sh
is automatically called and that copies the hook from a folder in the repository to the right place.
And since the hook is part of the repository it can be easily updated and distributed.
As noted by Hi-Angel in the comments:
I figured that out: Emacs has
build-aux
dir with hooks, and, upon runningautogen.sh
, all hooks are copied from there.
If your repository is an npm application, you can add this nice library as a dependency: Husky
File package.json
...
"devDependencies": {
...
"husky": ">=4"
},
"husky": {
"hooks": {
"pre-commit": "npm test"
}
}
pre-commit
can be basically any command: if it ends with exit code 0, the commit passes, otherwise the commit is interrupted.