我的 .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。知道我哪里出错了吗?
最佳答案
将 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 - '!'不排除 .gitignore 中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18781762/