How can I add my pre-commit hook to an existing git repository

I have a pre-commit hook and I want to add it to the repository so that by checking it out my colleagues have it instantly in place.
However if I try to add it ( being in the root directory of my project) I get the following result:

$ git add  .git/hooks/pre-commit
error: Invalid path '.git/hooks/pre-commit'
error: unable to add .git/hooks/pre-commit to index

Any idea if this work and how to achieve my goal?

  • How to find URL of local git repo
  • In Git, how do I get a detailed list of file changes from one revision to another?
  • Every time a co-worker commits a merge in git, all 10k+ files in the project have their line endings changed
  • Git - ignore on push/pull but not clone
  • Compilation Error when building Giraph
  • Right way to share a git repo over ssh
  • Git: Reset, remove commit permanently
  • Git - Find all descendant branches not merged in
  • Does `git push` work as shorthand for `git push origin newfeature`?
  • Why “ '/path/to/repo/.git/index.lock': File exists” issue happened?
  • Release management in SVN and Git
  • Git proxy failure need asistance
  • One Solution collect form web for “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 a composer install or composer 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.

    Git Baby is a git and github fan, let's start git clone.