如何仅从Git中的根文件夹中排除文件

Pav*_*kin 361 git gitignore

我知道使用.gitignore文件来排除一些正在添加的文件,但我config.php在源代码树中有几个文件,我只需要排除一个位于根目录中,而其他文件保持在版本控制之下.

我应该写什么.gitignore来实现这一目标?

Man*_*dan 537

文档:

如果模式不包含斜杠/,git将其视为shell glob模式,并检查相对于.gitignore文件位置的路径名的匹配(相对于工作树的顶层,如果不是来自.gitignore)文件).

前导斜杠与路径名的开头匹配.例如,"/*.c"匹配"cat-file.c"但不匹配"mozilla-sha1/sha1.c".

所以你应该在你的root中添加以下行.gitignore:

/config.php
Run Code Online (Sandbox Code Playgroud)

  • `/ folder /`或`/ folder`都可以使用,但在末尾添加斜杠只会限制与文件夹的匹配.如果你在根目录中有一个名为'foo'的文件,`/ foo /`就不会忽略它,但是`/ foo`会. (9认同)
  • 如果您已经提交了文件,请运行命令“git rm --cached <file>”,否则该文件将不会被忽略。来自:/sf/answers/89211321/ (4认同)
  • 谢谢你!我尝试了这种方法,但由于某种原因它不起作用。可能在某处输入错误=) (2认同)
  • 如果它不是文件,而是文件夹,并且我想忽略存储库根目录中的该文件夹、其包含的文件以及所有后代子文件夹及其文件怎么办?`/文件夹/`? (2认同)

Ric*_*arn 76

使用/config.php.


ken*_*pdx 26

旧版本的git需要先定义一个忽略模式,然后立即(在下一行)定义排除.[在1.9.3版本(Apple Git-50)上测试]

/config.php
!/*/config.php
Run Code Online (Sandbox Code Playgroud)

更高版本仅需要以下[在2.2.1版本上测试]

/config.php
Run Code Online (Sandbox Code Playgroud)


dru*_*gan 15

如果上述解决方案不适合您,请尝试以下方法:

#1.1 Do NOT ignore file pattern in  any subdirectory
!*/config.php
#1.2 ...only ignore it in the current directory
/config.php

##########################

# 2.1 Ignore file pattern everywhere
config.php
# 2.2 ...but NOT in the current directory
!/config.php
Run Code Online (Sandbox Code Playgroud)