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


当前回答

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

/.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中,有一个额外的斜线捕捉。排除.gitignore中的单个目录

目录_排除/

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

/

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

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

**/

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

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

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。我只是懒得处理这个测试用例。)

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

/.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”添加到要忽略的文件和目录列表中。

这对一些人来说可能非常明显,但我确实从其他答案中理解了这一点。

在目录中创建.gitignore文件本身没有任何作用。您必须将.gitignore作为文本文件打开,并将希望它忽略的文件/目录写在各自的行上。

所以cd到Git存储库目录

touch .gitignore
nano .gitignore

然后写下要忽略的文件和/或目录的名称及其扩展名(如果相关)。

此外,.gitignore在某些操作系统(例如Mac)上是一个隐藏文件,因此您需要ls-a来查看它,而不仅仅是ls。

在Unix上:

touch .gitignore

在Windows上:

echo > .gitignore

在终端中执行的这些命令将在当前位置创建.gitignore文件。

然后只需将信息添加到此.gitignore文件(例如使用Notepad++),哪些文件或文件夹应被忽略。保存更改。就是这样:)

更多信息:.gitignore