git 如何仅从Git中的根文件夹中排除文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3637660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to exclude file only from root folder in Git
提问by Pavel Karoukin
I am aware of using .gitignore
file to exclude some files being added, but I have several config.php
files in source tree and I need to exclude only one, located in the root while other keep under revision control.
我知道使用.gitignore
file 来排除一些被添加的文件,但我config.php
在源树中有几个文件,我只需要排除一个,位于根目录,而其他文件则保持在修订控制之下。
What I should write into .gitignore
to make this happen?
我应该写什么.gitignore
来实现这一点?
回答by Manoj Govindan
From the documentation:
从文档:
If the pattern does not contain a slash /, git treats it as a shell glob pattern and checks for a match against the pathname relative to the location of the .gitignore file (relative to the toplevel of the work tree if not from a .gitignore file).
A leading slash matches the beginning of the pathname. For example, "/*.c" matches "cat-file.c" but not "mozilla-sha1/sha1.c".
如果模式不包含斜杠 /,则 git 将其视为 shell glob 模式并检查与相对于 .gitignore 文件位置的路径名的匹配(如果不是来自 .gitignore 则相对于工作树的顶层)文件)。
前导斜杠与路径名的开头匹配。例如,“/*.c”匹配“cat-file.c”但不匹配“mozilla-sha1/sha1.c”。
So you should add the following line to your root .gitignore
:
因此,您应该将以下行添加到您的根目录中.gitignore
:
/config.php
回答by Richard Fearn
Use /config.php
.
使用/config.php
.
回答by kenhowardpdx
Older versions of git require you first define an ignore pattern and immediately (on the next line) define the exclusion. [tested on version 1.9.3 (Apple Git-50)]
旧版本的 git 要求您首先定义一个忽略模式,然后立即(在下一行)定义排除项。[在版本 1.9.3 (Apple Git-50) 上测试]
/config.php
!/*/config.php
Later versions only require the following [tested on version 2.2.1]
以后的版本只需要以下[在2.2.1版上测试]
/config.php
回答by drugan
If the above solution does not work for you, try this:
如果上述解决方案对您不起作用,请尝试以下操作:
#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