跳过Git提交钩子

本文翻译自:Skip Git commit hooks

I'm looking at a git hook which looks for print statements in Python code. 我正在寻找一个git钩子,它在Python代码中查找print语句。 If a print statement is found, it prevents the git commit. 如果找到print语句,则会阻止git commit。

I want to override this hook and I was told that there is a command to do so. 我想覆盖这个钩子,我被告知有一个命令可以这样做。 I haven't been able to find it. 我找不到它。 Any thoughts? 有什么想法吗?


#1楼

参考:https://stackoom.com/question/UL48/跳过Git提交钩子


#2楼

Maybe (from git commit man page ): 也许(从git commit手册页 ):

git commit --no-verify

-n  
--no-verify

This option bypasses the pre-commit and commit-msg hooks. 此选项绕过pre-commit和commit-msg挂钩。 See also githooks(5) . 另见githooks(5)

As commented by Blaise , -n can have a different role for certain commands. 正如Blaise 评论的那样, -n可以对某些命令有不同的作用。
For instance, git push -n is actually a dry-run push. 例如, git push -n实际上是一个干运行推送。
Only git push --no-verify would skip the hook. 只有git push --no-verify会跳过钩子。


Note: Git 2.14.x/2.15 improves the --no-verify behavior: 注意:Git 2.14.x / 2.15改进了--no-verify行为:

See commit 680ee55 (14 Aug 2017) by Kevin Willford (``) . Kevin Willford的 承诺680ee55 (2017年8月14日) (``)
(Merged by Junio C Hamano -- gitster -- in commit c3e034f , 23 Aug 2017) (由Junio C gitster合并- gitster - in commit c3e034f ,2017年8月23日)

commit : skip discarding the index if there is no pre-commit hook commit :如果没有pre-commit挂钩,则跳过丢弃索引

" git commit " used to discard the index and re-read from the filesystem just in case the pre-commit hook has updated it in the middle; git commit ”用于丢弃索引并从文件系统重新读取,以防pre-commit钩子在中间更新它; this has been optimized out when we know we do not run the pre-commit hook. 当我们知道我们没有运行pre-commit钩子时,这已被优化。


Davi Lima points out in the comments the git cherry-pick does not support --no-verify. DAVI利马指出, 在意见git cherry-pick 支持--no-验证。
So if a cherry-pick triggers a pre-commit hook, you might, as in this blog post , have to comment/disable somehow that hook in order for your git cherry-pick to proceed. 因此,如果一个樱桃挑选触发预提交钩子,你可能会像在这篇博客文章中那样以某种方式评论/禁用该钩子,以便你的git cherry-pick继续进行。
The same process would be necessary in case of a git rebase --continue , after a merge conflict resolution. git rebase --continue情况下,同样的过程是必要的 - 在合并冲突解决后继续。


#3楼

From man githooks : 来自man githooks

pre-commit 预提交
This hook is invoked by git commit, and can be bypassed with --no-verify option. 这个钩子由git commit调用,可以用--no-verify选项绕过。 It takes no parameter, and is invoked before obtaining the proposed commit log message and making a commit. 它不需要参数,并在获取建议的提交日志消息和进行提交之前被调用。 Exiting with non-zero status from this script causes the git commit to abort. 从此脚本退出非零状态会导致git commit中止。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
是的,你可以使用 Git 提交钩子自动添加 Change-Id,或手动添加它。下面是两种方法: 1. 自动添加 Change-Id:你可以使用 Git 提交钩子来自动添加 Change-Id。Git 提交钩子是一些自定义脚本,可以在 Git 操作期间触发。你可以创建一个提交钩子脚本,使其在每次提交时自动添加 Change-Id。这样,你就不必手动添加 Change-Id 了。这里是一个示例提交钩子脚本: ``` #!/bin/sh commit_msg_file=$1 commit_msg=$(cat $commit_msg_file) if ! grep -q "^Change-Id:" $commit_msg_file; then change_id=$(uuidgen) echo "Change-Id: $change_id" >> $commit_msg_file fi ``` 将该脚本保存为 `commit-msg` 文件,并将其放置在 `.git/hooks` 目录下。确保该文件具有执行权限。现在每次提交时,Git 将自动为你添加 Change-Id。 2. 手动添加 Change-Id:如果你不想使用提交钩子来自动添加 Change-Id,你可以手动添加它。要手动添加 Change-Id,请按照以下步骤操作: a. 生成一个唯一的 Change-Id:你可以使用任何工具来生成一个唯一的字符串。例如,你可以使用 `uuidgen` 命令来生成一个 UUID。保存生成的字符串。 b. 将 Change-Id 添加到提交信息中:在提交信息的末尾添加一行 `Change-Id: <change-id>`,其中 `<change-id>` 是你在第一步中生成的唯一字符串。 c. 保存提交信息并提交:保存提交信息并使用 `git commit` 命令提交更改。现在你的提交应该包含一个有效的 Change-Id。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值