501

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

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

4

5 回答 5

719

文档中:

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

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

因此,您应该将以下行添加到您的 root .gitignore

/config.php
于 2010-09-03T16:26:26.743 回答
96

使用/config.php.

于 2010-09-03T16:25:47.300 回答
36

旧版本的 git 要求您首先定义一个忽略模式并立即(在下一行)定义排除项。[在版本 1.9.3 (Apple Git-50) 上测试]

/config.php
!/*/config.php

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

/config.php
于 2015-01-17T14:44:44.437 回答
21

如果上述解决方案对您不起作用,请尝试以下操作:

#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
于 2014-09-18T16:34:22.967 回答
0

wordpress 站点的示例,但基本上忽略所有内容,然后添加以 ! 包括什么

# 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/
于 2021-12-12T14:05:22.943 回答