如何提交 git 钩子?

Lir*_*von 1 git

我正在尝试添加pre-commit脚本。

该脚本有效,但 git 忽略了对文件的更改,似乎 git 忽略了我.git.gitignore.

例如:

# add a hook that runs hello world
echo "#\!/bin/sh \n echo 'hello world'" > .git/hooks/pre-commit

# make the hook runnable
chmod +x .git/hooks/pre-commit

# check changes
git status 

# > Your branch is up to date with 'origin/master'.
# > nothing to commit, working tree clean
Run Code Online (Sandbox Code Playgroud)

因此,我如何将钩子提交到存储库?

Lir*_*von 9

找到了 -

在项目根目录中创建一个目录,我们称之为git_hooks,向其中添加脚本,并使用 .git 将目录设置为 git hooks 目标git config

# create directory
mkdir git_hooks

# add a hook that runs hello world
echo "#\!/bin/sh \n echo 'hello world'" > git_hooks/pre-commit

# make the hook runnable
chmod +x git_hooks/pre-commit

# configure git to use the new hook - it will stay with the repository
git config core.hooksPath "./git_hooks"

# the script will run on every commit:
git commit -am "added pre-commit hook"
> hello world
Run Code Online (Sandbox Code Playgroud)


Aco*_*orn 2

.git存储库不是存储库的一部分。它存储库。

如果您想保存存储库的配置,我建议您使用另一个 Git 存储库来实现此目的。

  • 那么如何添加钩子呢?我是否必须添加另一个目录并强制对其进行符号链接?添加钩子的正确方法是什么?这就是每个教程向我展示的内容,除非我使用一些奇特的框架,例如 python pre-commit 或 js husky? (5认同)