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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 09:02:09  来源:igfitidea点击:

How to exclude file only from root folder in Git

gitgitignore

提问by Pavel Karoukin

I am aware of using .gitignorefile to exclude some files being added, but I have several config.phpfiles in source tree and I need to exclude only one, located in the root while other keep under revision control.

我知道使用.gitignorefile 来排除一些被添加的文件,但我config.php在源树中有几个文件,我只需要排除一个,位于根目录,而其他文件则保持在修订控制之下。

What I should write into .gitignoreto 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