如果存在未跟踪的文件,如何禁止 git 提交?

发布于 2024-09-15 19:50:25 字数 124 浏览 4 评论 0原文

有时,当存在未跟踪的文件时,禁止 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半透明的墙 2024-09-22 19:50:25

您可以添加一个 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 with git 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:$'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文