Cherry
Cherry

Reputation: 33628

How specify new path for git hooks directory?

I have a git installed in "Programm files" directory and I have not modified rights to it (only reading). But i want to put some new hooks. Is there a way to call git in command line and specify new path for hooks directory?

Upvotes: 5

Views: 6024

Answers (1)

VonC
VonC

Reputation: 1329652

Git hooks are local to a repo.

Once you have created a repo (git init yourRepo), you will see those hooks in yourRepo/.git/hooks.


Update May 2016 for git 2.9 (June 2016)

You have a new config: core.hooksPath

See commit 867ad08, commit de0824e, commit bf7d977, commit 49fa52f (04 May 2016) by Ævar Arnfjörð Bjarmason (avar).
(Merged by Junio C Hamano -- gitster -- in commit 6675f50, 17 May 2016)

core.hooksPath

By default Git will look for your hooks in the '$GIT_DIR/hooks' directory.
Set this to different path, e.g. '/etc/git/hooks', and Git will try to find your hooks in that directory, e.g. '/etc/git/hooks/pre-receive' instead of in '$GIT_DIR/hooks/pre-receive'.

The path can be either absolute or relative. A relative path is taken as relative to the directory where the hooks are run

The "relative path" part comes from the Git Hooks man page:

Before Git invokes a hook, it changes its working directory to either the root of the working tree in a non-bare repository, or to the $GIT_DIR in a bare repository.


Original answer (Feb. 2014)

You could specify a different templatedir (from git config)

init.templatedir

Specify the directory from which templates will be copied. (See the "TEMPLATE DIRECTORY" section of git-init(1).)

The default template directory includes some directory structure, some suggested "exclude patterns", and copies of sample "hook" files.
The suggested patterns and hook files are all modifiable and extensible.

That would mean a new repo (git init) would fill the default content (including default sample hooks) from a custom folder instead of the default one (which is part of your git installation).


Roelant asks in the comments:

What is the use case for saving hooks outside of the git repository?

Storing Git hooks outside a repository can be beneficial for various use cases.

  1. Shared Hooks:

    • If you have a set of hooks that you want to share across multiple repositories, having a centralized hooks' directory can be beneficial. This way, you maintain a single version of each hook, making updates easier and ensuring consistency across projects.
    • Commands:
      git config --global core.hooksPath /path/to/shared/hooks
      
    • Centralizing hooks can simplify maintenance. When hooks are updated, the changes are automatically available to all repositories that use the centralized hooks' directory, without needing to copy changes to each repository individually.
    • In some organizations, there might be policies or compliance requirements that dictate how and where hooks should be stored and managed.
  2. Permission Control / Security:

    • In some environments, you might want to exert tighter control over what hooks are being executed. By placing hooks in a directory outside the repository, you can set the necessary permissions to control who can add or modify hooks.
    • Example Permissions (Unix/Linux):
      chown -R root:root /path/to/shared/hooks
      chmod -R 755 /path/to/shared/hooks
      
    • Keeping hooks outside the repository can be a security measure to prevent malicious code from being executed if someone gains unauthorized access to your repository.
  3. Version Control:

    • You might want to version control your hooks separately from your project code. By placing hooks in a separate repository, you can manage them with their own version control, issues, and pull requests.
    • Create a separate repository for hooks:
      git init /path/to/shared/hooks
      
+----------------------+      +---------------------+
|   Individual Repo    |      |  Centralized Hooks  |
| (e.g., RepoA, RepoB) |      | Directory           |
+----------------------+      +---------------------+
            |                               |
            | Configure to use              |
            | centralized hooks             |
            +------------------------------>|
                                            |
                                  [Hooks Execution]

By configuring the core.hooksPath to point to a centralized or shared directory, you can address the above scenarios effectively.

Upvotes: 8

Related Questions