如果存在未跟踪的文件,如何禁止 git 提交?
有时,当存在未跟踪的文件时,禁止 git commit 是有意义的,因为您应该将它们添加到要提交的文件或 gitignore 中。并且像 -f 这样的选项来强制此提交也是必要的。那么有没有任何选项/插件/等等可以做到这一点?非常感谢。
Sometimes, it makes sense to forbid git commit when there are untracked files, because you should add them either to files to commit or gitignore. And an option like -f to force this commit is also necessary. So is there any option/plugin/etc to do that? Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以添加一个 pre-commit 挂钩,如果发现未跟踪的文件,该挂钩会以非零值退出(您可以检查
git status - 的输出-porcelain -u
来确定它们的存在——查找以“?”开头的行)。然后,如果您不关心未跟踪的文件,则可以使用 git commit --no-verify 覆盖提交验证。编辑:感谢@Jefromi;我手头没有git环境来测试。他还指出,该命令应该识别是否存在未跟踪的文件: git status -u | grep '^# 未跟踪的文件:$'。
You can add a pre-commit hook that exits with a non-zero value if you find untracked files (you could examine the output of
git status --porcelain -u
to determine their presence--look for lines beginning with "?"). Then you can override the commit verification withgit commit --no-verify
if you don't care about the untracked files.Edit: Thanks to @Jefromi; I don't have my git environment at hand to test. He also notes that this command should identify the presence of untraced files:
git status -u | grep '^# Untracked files:$'
.