git - '!'不排除 .gitignore 中的文件

标签 git gitignore

我的 .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/*

这将忽略目录中的所有文件。在大多数情况下,temptemp/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/

相关文章:

wordpress - github上的公共(public)密码

asp.net-mvc - 在 github 中避免 ASP.NET 密码?

git - 为什么 Git 忽略这个不在 .gitignore 中的目录?

Git 忽略和 Maven 目标

git - 尝试从 Bitbucket 迁移到 Github,出现文件大小错误

git - 如何在裸仓库上运行 git p4 Submit?

git - 如何使用 PhpStorm 和 Git 忽略文件?

git - 如何 Git 忽略这些文件

laravel-5.3 - 如何在 Laravel storage/app/mydir/myfile.json 中不忽略

git 'failed to read object' 和 'unable to find' 错误