'';不排除.gitignore中的文件

'';不排除.gitignore中的文件,git,gitignore,Git,Gitignore,我的.gitignore文件: .DS_Store temp !temp/script.txt # 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
文件:

.DS_Store
temp
!temp/script.txt
# 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 status
时,它不会显示
temp/
目录以指示
script.txt
未处于暂存状态。它仅显示
.gitignore
文件:

.DS_Store
temp
!temp/script.txt
# 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 add…”更新将提交的内容)
#(使用“git签出--…”放弃工作目录中的更改)
#
#修改:.gitignore

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

用以下内容替换
temp

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

为了说明这一点,当
temp/
具有
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

注意:git中没有对目录进行版本控制

使用.gitof

temp/*
!temp/script.txt

请注意,顺序非常重要(我先搞错了,这就是为什么我几乎得出结论说它无法完成)

您不能使用
.gitignore
忽略一个已经由Git版本控制的文件。@jszakmeisterhere@sehe你可能是对的(这就是为什么我没有把它写下来作为答案),但是双重否定“它不显示temp/目录以表明script.txt不在staged中”听起来像是工作树的一部分。注意,您必须非常仔细地观察包含/排除的顺序。此外,
temp
将忽略名为
temp
的文件,
temp/
仅忽略(任何)那里面的文件directory@sehe是的,我一定要把它加进去。为了避免混淆,我把它排除在外了。