Reputation: 33628
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
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.
Shared Hooks:
git config --global core.hooksPath /path/to/shared/hooks
Permission Control / Security:
chown -R root:root /path/to/shared/hooks
chmod -R 755 /path/to/shared/hooks
Version Control:
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