【问题标题】:How can I commit Git hooks? [duplicate]如何提交 Git 钩子? [复制]
【发布时间】:2019-12-26 17:53:07
【问题描述】:

我正在尝试添加一个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

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

【问题讨论】:

标签: git


【解决方案1】:

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

如果你想保存存储库的配置,我建议你使用另一个 Git 存储库。

【讨论】:

  • 那么如何添加钩子呢?我是否必须添加另一个目录并强制对其进行符号链接?添加钩子的正确方法是什么?这是每个教程都向我展示的,除非我使用一些花哨的框架,如 python pre-commit 或 js husky?
【解决方案2】:

我找到了-

在项目根目录下创建一个目录,我们称之为git_hooks。将脚本添加到其中,并使用 git config 将目录设置为 Git 挂钩目标。

# 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!

【讨论】:

    猜你喜欢
    • 2013-12-15
    • 2012-06-06
    • 2018-01-07
    • 2019-07-12
    • 2015-01-13
    • 2018-05-21
    • 1970-01-01
    • 2020-03-09
    相关资源
    最近更新 更多