我知道使用 .gitignore
文件来排除某些文件被添加,但是我在源代码树中有几个 config.php
文件,我需要排除其中一个,它位于根目录下,而其他文件要保留进行版本控制。
我应该在 .gitignore
文件中写什么内容来实现这一点?
/config.php
,这将忽略位于根目录下的config.php文件,而其他的config.php文件将继续进行版本控制。从文档中得知:
如果模式不包含斜杠 /,git 将其视为 shell glob 模式,并针对路径名相对于 .gitignore 文件的位置(如果不是来自 .gitignore 文件,则相对于工作树的顶级)检查匹配项。
前导斜杠匹配路径名的开头。例如,“/*.c”与“cat-file.c”匹配但不与“mozilla-sha1/sha1.c”匹配。
因此,您应该将以下行添加到根目录下的 .gitignore
文件中:
/config.php
请使用/config.php
文件。
旧版本的git要求您首先定义一个忽略模式,然后立即(在下一行)定义排除。 [在版本1.9.3(Apple Git-50)上测试]
/config.php
!/*/config.php
后续版本只需要以下步骤[在2.2.1版本上进行了测试]
/config.php
如果上述解决方案对您不起作用,请尝试这个:
#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
/config.php
却没有。我很好奇为什么会这样。你有什么想法吗? - iago-lito~/.gitignore
文件而有所不同,对吧?此外,我的观点是:你写道“如果上述解决方案不起作用”,为什么会不起作用呢? - iago-lito# Ignore everything in the root except the "wp-content" directory.
/*
!.gitignore
!wp-content/
!wp-config.php
#
# # Ignore everything in the "wp-content" directory, except the "plugins"
# # and "themes" directories.
wp-content/*
!wp-content/plugins/
!wp-content/themes/
#
# # Ignore everything in the "plugins" directory, except the plugins you
# # specify (see the commented-out examples for hints on how to do this.)
wp-content/plugins/*
# # !wp-content/plugins/my-single-file-plugin.php
# # !wp-content/plugins/my-directory-plugin/
#
# # Ignore everything in the "themes" directory, except the themes you
# # specify (see the commented-out example for a hint on how to do this.)
wp-content/themes/*
!wp-content/themes/twentyeleven/
!wp-content/themes/twentyeleven/
,而不是写成这样:!wp-content/
、wp-content/*
、!wp-content/themes/
、wp-content/themes/*
、!wp-content/themes/twentyeleven/
。这样做的效果是一样的,对吧? - ADTC
/folder/
? - CodeManX/folder/
还是/folder
都可以使用,但在末尾添加斜杠会将匹配限制为仅文件夹。如果您在根目录中有一个名为“foo”的文件,则/foo/
不会忽略它,但/foo
会忽略它。 - tehDorfgit rm --cached <file>
,否则该文件不会被忽略。来源:https://dev59.com/pXM_5IYBdhLWcg3wrFMt#1274447 - Bruno Pologit rm --cached <file>
!这个方法对我有效。 - TangHongWan