利用 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
我的脚本必须从实际的 git_hooks/ 目录中运行。如果您愿意,您可以修改它以使其行为不同。
此脚本将对 git_hooks/ 目录中没有后缀的任何文件进行符号链接。我在这个目录中有一个 README.txt + 这个脚本(名为 symlink.sh)。所有实际的 git 钩子都被命名为“pre-commit”、“pre-push”等,因此它们将被符号链接。