3

我的.gitignore文件:

.DS_Store
temp
!temp/script.txt

但是,当我这样做时git status,它不会显示temp/目录以指示该目录script.txt未上演。它只显示.gitignore文件:

# Changes not staged for commit:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#   modified:   .gitignore

我只是在学习git。知道我哪里出错了吗?

4

2 回答 2

3

替换temp为:

temp/*

这将忽略目录中的所有文件。在大多数情况下temp,所有工作都一样temp/temp/*因为在前两种情况下,目录被忽略,而在第三种情况下,Git 不跟踪空目录。因此,目录本身将被忽略。

为了说明这一点,这基本上是当temp/has时发生的情况temp1.txt temp2.txt temp3.txt

temp # anything with the name of "temp"; a directory, a symlink, a file.
temp/ # whole of temp dir
temp/* # temp/temp1.txt temp/temp2.txt temp/temp3.txt

因此,第三种情况将适用于您的情况。

否定模式必须放在覆盖它的模式之后。在这种情况下,!temp/script.txt必须在之后temp/*。否则,将不会被应用。

因此,您的最终 .gitignore 文件应如下所示:

.DS_Store
temp/*
!temp/script.txt
于 2013-09-13T08:43:14.190 回答
0

注意目录在 git 中没有版本控制

使用 .gitignore 的

temp/*
!temp/script.txt

请注意,顺序非常重要(我首先错了,这就是为什么我几乎断定它无法完成)

于 2013-09-13T08:44:23.800 回答