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?

  • git: Your branch is Ahead by X commits
  • How do I do an initial push to a remote repository with Git?
  • Spring boot - Spring Cloud Config Server EXCEPTION
  • How to use Sublime text editor to resolve git merge conflicts?
  • Git reset and checkout by single command
  • Use of an exclamation mark in a Git commit message via command line?
  • Error in setting up .SSH keys with Windows PowerShell
  • Git: list all tracked files
  • Why do my GitHub commits always show as unknown author?
  • Chef will not checkout development branch from git
  • Unable to remove fast many files in Git
  • How do I get a remote tracking branch to stay up to date with remote origin in a bare Git repository?
  • 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" ] }, 

    So on dev, 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.