[英]Bypass pre-commit hook during `git revert --continue`
I was doing a git revert
(of a previous revert), which caused some merge conflicts.我正在做一个
git revert
(以前的恢复),这导致了一些合并冲突。 After resolving the conflicts my pre-commit hook threw some code sniffer issues.解决冲突后,我的预提交钩子抛出了一些代码嗅探器问题。
Since these code sniffer notices are fixed elsewhere I wanted to bypass the pre-commit hook at this point using git revert --continue --no-verify
, apparently git revert
doesn't have the --no-verify
subcommand.由于这些代码嗅探器通知已在其他地方修复,因此我想此时使用
git revert --continue --no-verify
绕过预提交钩子,显然git revert
没有--no-verify
子命令。
git revert
does have a --no-commit
subcommand but this doesn't work in combination with --continue
. git revert
确实有一个--no-commit
子命令,但这不能与--continue
结合使用。
I ended up renaming the pre-commit
file, but out of curiosity.我最终重命名了
pre-commit
文件,但出于好奇。 Is there any better wat to bypass pre-commit hooks at that point?那时有没有更好的方法可以绕过预提交挂钩?
If you are able to change the hooks , you can just add a toggle for each one.如果您能够更改hooks ,则只需为每个钩子添加一个开关即可。 Or just use a script to temporarily rename a given hook , as commented .
或者只是使用脚本临时重命名给定的钩子, 如注释。
Either way will selectively skip the problematic hook while letting the other ones run normally.无论哪种方式都会有选择地跳过有问题的钩子,同时让其他钩子正常运行。 This ensures that other checks occur (if they exist), such as a commit message validation with the
commit-msg
hook .这确保发生其他检查(如果存在),例如使用
commit-msg
hook的提交消息验证。
If that does not apply, there is an alternative:如果这不适用,还有一个替代方案:
Usually, when an operation is stopped by a conflict, after fixing it you can just run通常,当操作因冲突而停止时,修复它后您可以运行
git commit
instead of代替
git $operation --continue
This applies to revert
, merge
, cherry-pick
and possibly others (although in rebase
it might behave differently, since it is a sequence of operations).这适用于
revert
、 merge
、 cherry-pick
和其他可能的情况(尽管在rebase
它的行为可能不同,因为它是一系列操作)。
So, as noted, to bypass the hooks , you can just add --no-verify
:因此,如前所述,要绕过hooks ,您只需添加
--no-verify
:
git commit --no-verify
Note : The feature above seems to be undocumented, but it WorksForMe(tm).注意:上面的功能似乎没有记录,但它适用于 WorksForMe(tm)。
From a diff of strace git commit
and strace git revert --continue
, the former does a lot of other things (515 vs 173 lines, respectively), such as checking if a rebase is in progress and creating some temporary files in $GIT_DIR/objects
.从
strace git commit
和strace git revert --continue
的差异strace git revert --continue
,前者做了很多其他的事情(分别为 515 行和 173 行),例如检查 rebase 是否正在进行以及在$GIT_DIR/objects
创建一些临时文件$GIT_DIR/objects
。 Example:例子:
stat(".git/MERGE_HEAD", 0x7ffefb5d0760) = -1 ENOENT (No such file or directory)
stat(".git/rebase-apply", 0x7ffefb5d0620) = -1 ENOENT (No such file or directory)
stat(".git/rebase-merge", 0x7ffefb5d0620) = -1 ENOENT (No such file or directory)
stat(".git/CHERRY_PICK_HEAD", 0x7ffefb5d0760) = -1 ENOENT (No such file or directory)
stat(".git/BISECT_LOG", 0x7ffefb5d0620) = -1 ENOENT (No such file or directory)
stat(".git/REVERT_HEAD", {st_mode=S_IFREG|0644, st_size=41, ...}) = 0
lstat(".git/REVERT_HEAD", {st_mode=S_IFREG|0644, st_size=41, ...}) = 0
openat(AT_FDCWD, ".git/REVERT_HEAD", O_RDONLY) = 4
But the result appears to be same: They open up the editor with the git-generated message (eg: "This reverts commit [...]") and commit after exiting.但结果似乎是一样的:他们用 git 生成的消息打开编辑器(例如:“This reverts commit [...]”)并在退出后提交。 I have used the alternative form multiple times and never had any problems at least (by the way, I probably discovered it under the same scenario).
我多次使用替代形式,至少从未遇到任何问题(顺便说一句,我可能在同一场景下发现了它)。
There is the possibility to check inside of your pre-commit hook, if you are in a certain state.如果您处于某种状态,则可以检查预提交钩子的内部。 Based on that check you can abort the script.
根据该检查,您可以中止脚本。 The following commands will return a hash when they are in a state like that, respectively, inside a revert, merge and rebase:
以下命令将在它们处于类似状态时分别返回一个哈希值,分别在还原、合并和变基中:
git rev-parse -q --verify REVERT_HEAD
git rev-parse -q --verify MERGE_HEAD
git rev-parse -q --verify REBASE_HEAD
This is how I use two of them in my pre-commit hook:这就是我在预提交钩子中使用其中两个的方式:
#!/bin/sh
IN_MERGE=$(git rev-parse -q --verify MERGE_HEAD)
if [ "$IN_MERGE" != "" ]
then
exit 0;
fi
IN_REBASE=$(git rev-parse -q --verify REBASE_HEAD)
if [ "$IN_REBASE" != "" ]
then
exit 0;
fi
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.