我正在尝试添加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)
因此,我如何将钩子提交到存储库?
找到了 -
在项目根目录中创建一个目录,我们称之为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)
该.git存储库不是存储库的一部分。它是存储库。
如果您想保存存储库的配置,我建议您使用另一个 Git 存储库来实现此目的。