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?

  • Cannot push local Git repo to Visual Studio Online project
  • git rebase fatal: Needed a single revision invalid upstream –i
  • Git is ignoring files that aren't in gitignore
  • Why git can't merge renames with modifications?
  • How to connect existing Android Studio project to existing Github repository
  • How can I define an alias for a Git subcommand (e.g. for `list` in `git stash list`)?
  • Sync local branch with remote branch egit
  • Git ref master now empty, how to recover?
  • git show remote repository (display the contents of a single file)
  • How to resolve stderr: fatal: Not a valid object name HEAD in jenkins?
  • How to have meld as git mergetool to only show conflict and not differences?
  • How to auto-detect if a Git commit or rebase contains something like “<<<<< HEAD”?
  • 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.