Linux git 中钩子的符号链接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4592838/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Symbolic link to a hook in git
提问by Mateusz Dymczyk
I wrote my own custom post-merge hook, now I added a "hooks" directory to my main project folder (since git doesn't track changes in .git/hooks), somewhere I read that I can make a symbolic link from hooks to .git/hooks so I don't have to copy the file from one folder to the other every time someone changes it so I tried:
我编写了自己的自定义合并后挂钩,现在我在主项目文件夹中添加了一个“hooks”目录(因为 git 不跟踪 .git/hooks 中的更改),我在某处读到我可以从挂钩创建符号链接到 .git/hooks 这样我就不必每次有人更改文件时都将文件从一个文件夹复制到另一个文件夹,所以我尝试了:
ln -s -f hooks/post-merge .git/hooks/post-merge
ln -s -f hooks/post-merge .git/hooks/post-merge
But it doesn't seem to work, any ideas why? "ln hooks/post-merge .git/hooks/post-merge" works fine but making a hard link is the same as copyin I guess....
但它似乎不起作用,任何想法为什么?“ln hooks/post-merge .git/hooks/post-merge”工作正常,但制作硬链接与 copyin 我猜是一样的......
采纳答案by Michal ?iha?
you just used wrong path, it should be:
您刚刚使用了错误的路径,应该是:
ln -s -f ../../hooks/post-merge .git/hooks/post-merge
回答by Jekis
Changing directory before linking
链接前更改目录
cd /path/to/project-repo/.git/hooks
ln -s -f ../../hooks/post-merge ./post-merge
回答by Frazko
why not just cp ./hooks/* .git/hooks/
为什么不只是 cp ./hooks/* .git/hooks/
this worked for me in Mac OS
这在 Mac OS 中对我有用
回答by swayamraina
The path calculation is done relative to the symlink. Let's understand using an example,ln -s path/to/file symlink/file
路径计算是相对于符号链接完成的。让我们通过一个例子来理解,ln -s path/to/file symlink/file
Here, the path to the file should actually be the relative path from the symlink path.
The system actually calculates the file path as symlink/path/path/to/file
The above command should be re-written as ln -s ../path/to/file symlink/path
The folder structure being,
在这里,文件的路径实际上应该是符号链接路径的相对路径。
系统实际计算文件路径为symlink/path/path/to/file
上面的命令应该改写为ln -s ../path/to/file symlink/path
文件夹结构为,
/code
------ symlink/file
------ path/to/file
/code
------ 符号链接/文件
------ 路径/到/文件
回答by Pierre.Sassoulas
While you can use symbolic links, you can also change the hooks folder for your project in your git settings with :
虽然您可以使用符号链接,但您还可以使用以下命令在您的 git 设置中更改项目的 hooks 文件夹:
git config core.hooksPath hooks/
Which is local by default so it won't ruin git hooks for your other projects. It works for all hook in this repository, so it's especially useful if you have more than one hook.
默认情况下是本地的,因此它不会破坏其他项目的 git hook。它适用于这个存储库中的所有钩子,所以如果你有多个钩子,它特别有用。
If you already have custom hooks in .git/hooks/
that you do not want to share with your team you can add them in hooks/ and add a .gitignore
so they're not shared.
如果您已经有自定义钩子.git/hooks/
,您不想与您的团队共享,则可以将它们添加到 hooks/ 并添加一个,.gitignore
以便它们不被共享。
回答by cchoe1
Utilizing Michael Cihar's comment, here is an example of a bash script I wrote to simply create these symlinks. This script is located in git_hooks/ dir which is at the project root. My .git/ folder is also in the same directory level.
利用 Michael Cihar 的评论,这是我编写的一个 bash 脚本示例,用于简单地创建这些符号链接。该脚本位于项目根目录下的 git_hooks/ 目录中。我的 .git/ 文件夹也在同一目录级别。
#!/usr/bin/env bash
pwd=$(pwd);
# Script is designed to be ran from git_hooks/ dir
if [[ "$pwd" == *"git_hooks"* ]]; then
files=$(ls | grep -v -e '.*\.');
while read -r file; do
ln -s ../../git_hooks/$file ../.git/hooks/
echo "Linked $file -> ../.git/hooks/$file"
done <<< "$files";
else
echo "";
echo "ERROR: ";
echo "You must be within the git_hooks/ dir to run this command";
exit 1;
fi
My script must be ran from within the actual git_hooks/ directory. You can modify it to behave differently, if you'd like.
我的脚本必须在实际的 git_hooks/ 目录中运行。如果您愿意,您可以修改它以使其表现不同。
This script will symlink any file that is not suffixed with a file extension within the git_hooks/ directory. I have a README.txt in this directory + this script (named symlink.sh). All the actual git hooks are named 'pre-commit', 'pre-push', etc. so they will be symlinked.
此脚本将符号链接任何不以 git_hooks/ 目录中的文件扩展名作为后缀的文件。我在这个目录中有一个 README.txt + 这个脚本(名为 symlink.sh)。所有实际的 git 钩子都被命名为“预提交”、“预推送”等,因此它们将被符号链接。