如何在 git 中执行 "no spaces in filenames" 政策?

How to enforce a "no spaces in filenames" policy in git?

正如标题所说。我希望 git 拒绝 add 文件名中包含空格的文件。是否有强制执行此操作的配置?

**\ **的正则表达式可以在.gitignore文件中使用 用于拒绝带有 spaces.

的文件名

下面是一个工作示例的演示:

第一步:.gitignore文件中添加**\ **存储库

$ my-repository (master)
$ ls
core/  it.tests/  pom.xml  README.md  ui.apps/  ui.content/

$ cat .gitignore
**/.classpath
**/target
**/node_modules/
/node_modules/
/build/

**\ ** 

          ---> Ignore file that has blank spaces in the name

第 2 步: 创建一个名称中包含空白 spaces 的文件(示例:x y z.txt)

$ echo "Hello! How are you doing?" > "x y z.txt"

$ ls
 core/   it.tests/   pom.xml   README.md   ui.apps/   ui.content/

 'x y z.txt'

第 3 步:尝试添加 文件,名称为 space 到存储库并提交

$ git add .

$ git commit -a

On branch master
Your branch is up to date with 'origin/master'.

nothing to commit, working tree clean

结果:

.gitignore 文件中 **\ ** 的模式阻止名称中包含空白 space 的文件从被添加到存储库。

关于 .gitignore 文件的更多信息:

https://git-scm.com/docs/gitignore

I want git to refuse to add files which contain spaces in filenames.

您实际上要问的是如何防止将某些文件添加到索引中。使用 Git 挂钩目前无法满足您的具体要求。

也许您想申请一个 pre-add 挂钩?这对我来说似乎很合理,而且您不是第一个提出要求的人。我建议与 Git community 进行进一步讨论。

近期/优先事项

现在,如果你想在短期内解决你的问题,我建议我们退后一步。以下目标中有一个比另一个重要吗?

防止文件被...

  1. ...添加索引? (git add)

  2. ...已提交存储库? (git commit)

拒绝提交

据我所知,阻止提交是这里的核心要求。

因此,向前看,此答案将问题解释为:“我希望 git 拒绝 提交 文件名中包含空格的文件。

我看到两类答案:(a) 常用答案和 (b) 不常用答案

常用:预提交钩子

实际上,我建议使用 Git pre-commit hook:

The pre-commit hook is run first, before you even type in a commit message. It’s used to inspect the snapshot that’s about to be committed, to see if you’ve forgotten something, to make sure tests run, or to examine whatever you need to inspect in the code. Exiting non-zero from this hook aborts the commit, although you can bypass it with git commit --no-verify. You can do things like check for code style (run lint or something equivalent), check for trailing whitespace (the default hook does exactly this), or check for appropriate documentation on new methods.

我看到两种方法:

  1. shell(无额外依赖):开源GitHooks documentation has an example pre-commit shell script.

  2. 一个框架:我推荐pre-commit。 (这不包含在 git 中。)

pre-commit: A framework for managing and maintaining multi-language pre-commit hooks

为什么要创建预提交框架?

As we created more libraries and projects we recognized that sharing our pre-commit hooks across projects is painful. We copied and pasted unwieldy bash scripts from project to project and had to manually change the hooks to work for different project structures.

不太常见的方法

  1. 如果您可以控制环境,您可以考虑在 git add.

    之上编写自己的包装器
  2. 有关 (a) 涂抹和清洁的讨论,请参阅 How to automatically invoke a script before a git add?; (b) git-gulp; (c) 可能随着时间的推移会添加其他内容。

为什么 .gitignore 不起作用 无法阻止提交

更新:我添加了删除线文本来指出我之前编辑的错误。 粗体斜体字.

中标注了更正和更改
  • 一个.gitignore文件不会阻止提交文件;相反,它会阻止 未跟踪的 文件被跟踪。

  • git add file.txt 总是会成功地将 file.txt 添加到索引中,无论 .gitignore 的内容如何

  • 更正: git add file.txt 如果 file.txt 匹配 .gitignore

  • 说明: git add -f file.txt总是会成功添加file.txt到索引, 无论 .gitignore

    的内容如何
  • 加法:因为有git add -f命令,可以看到.gitignore 只能警告,不能阻止文件被添加到索引(又名暂存)。文件暂存后,提交就很简单了。因此,在这个答案的上下文中,目标是 防止 提交(见上文),.gitignore 是不够的。

  • 一旦文件被跟踪,就可以提交 暂存git commit file.txt -- 无论 .gitignore.

  • 详细说明: 一旦文件被跟踪,就可以使用 git add file.txt 进行暂存并提交 git commit -- 不管 .gitignore.