如何在Windows上使用msysgit忽略Git中的目录或文件夹?


当前回答

我认为问题是你的工作树是这样的:

a-cache/foo
a-cache/index.html
b-cache/bar
b-cache/foo
b-cache/index.html
.gitignore

…用你描述的.gitignore。这将为您提供git状态输出,如:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    .gitignore
#    a-cache/
#    b-cache/

…如果index.html文件尚未添加到存储库中。(Git发现缓存目录中有未忽略的文件,但它只报告目录。)要解决此问题,请确保您已添加并提交了index.html文件:

git add *cache/index.html
git commit -m "Adding index.html files to the cache directories"

…然后您的git状态将如下所示:

$ git status
# On branch master
# Untracked files:
#   (use "git add <file>..." to include in what will be committed)
#
#    .gitignore
nothing added to commit but untracked files present (use "git add" to track)

(很明显,你也希望提交.gitignore。我只是懒得处理这个测试用例。)

其他回答

在Windows中,有一个额外的斜线捕捉。排除.gitignore中的单个目录

目录_排除/

将可能工作,但排除所有具有

/

当目录中的文件名带有空格(如my-file.txt)时会导致问题:GitBash用反斜杠(如my\file.txt)转义这些空格,而Git for Windows无法区分/和\。

要排除所有目录,最好使用:

**/

两个连续的星号表示目录内容。

当其他一切都失败时,尝试编辑文件

/.git/info/exclude

并将您想要的目录添加到文件末尾,如下所示:

# git ls-files --others --exclude-from=.git/info/exclude
# Lines that start with '#' are comments.
# For a project mostly in C, the following would be a good set of
# exclude patterns (uncomment them if you want to use them):
# *.[oa]
# *~
assets/
compiled/

我将文件夹“assets”和“compiled”添加到要忽略的文件和目录列表中。

我在Windows资源管理器中使用创建文件时遇到一些问题。在开始时。

解决方法是进入commandshell并使用“edit”创建一个新文件。

如果您想维护一个文件夹而不是其中的文件,只需在文件夹中放置一个“.gitignore”文件,其中包含“*”作为内容。此文件将使Git忽略存储库中的所有内容。但是.gitignore将包含在您的存储库中。

$ git add path/to/folder/.gitignore

如果您添加了一个空文件夹,则会收到此消息(.gitignore是一个隐藏文件)

The following paths are ignored by one of your .gitignore files:
path/to/folder/.gitignore
Use -f if you really want to add them.
fatal: no files added

因此,使用“-f”强制添加:

$ git add path/to/folder/.gitignore -f

似乎忽略文件和目录有两种主要方式:

.git忽略将.gitignore文件放置到存储库的根目录中,而不是.git文件夹(在Windows中,确保看到真正的文件扩展名,然后使.gitignore.(结尾处的点为空文件扩展名))生成全局配置~/.gitignore_global并运行git-config--global-core.excludsfile~/.gigignore_global将其添加到git配置中注意:以前跟踪的文件可以通过运行gitrm--缓存文件名来取消跟踪存储库排除-对于不需要共享的本地文件,只需将文件模式或目录添加到文件.git/info/exclude中即可。这些规则未提交,因此其他用户看不到。这里有更多信息。

要在忽略文件列表中设置例外,请参阅此问题。