git 中列出的文件的状态显示 "Changes not staged for commit"。git忽略?

git status is showing "Changes not staged for commit" for files listed in .gitignore?

让我们看看 .gitignore 文件 - 我在其中添加了 mllib/pom.xml 和 pom.xml 甚至 .gitignore (这不应该是必要的 - 所以有些不对劲..):

$head .gitignore
.gitignore
mllib/pom.xml
pom.xml

那么让我们看看git要添加什么文件:

$ git status
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
    modified:   mllib/pom.xml
    modified:   

UPDATE 关于not "ignoring the .gitignore"有两条评论。但是在删除 .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
    modified:   mllib/pom.xml

所以 (a) .gitignore 出现了,并且 (b) 真正重要的特定文件不要添加到提交中 - mllib/pom.xml - 出现了。

.gitignore 文件并不像您认为的那样。1

特别是,一旦您将文件 "known" 添加到 git,因为它在索引中,将该文件的名称添加到 .gitignore 没有任何效果。 Git 已经跟踪该文件,它将继续跟踪它。

本质上,.gitignore 文件实际上并不是要忽略的文件列表。相反,当 git 遇到 "untracked" 文件的情况并准备向您报告时,.gitignore 内容是它应该隐藏的名称列表。

对于 .gitignore 文件本身,您可能只想 git add 更改并提交它们,因为作为一般规则,任何克隆您的存储库的人都可能希望忽略同一组未跟踪文件, 所以 .gitignore 应该被跟踪和版本控制。

然而,对于 XML 文件,您可能 "generated content" 不希望版本控制,但仍希望将其保留在工作树中。这有点问题,因为 git 已经在跟踪它并将坚持继续对该文件进行版本控制。

此时你可以做的是将其从 git 的索引中删除,而不是从工作树中删除:

git rm --cached mllib/pom.xml

就目前而言这很好(git 从索引中删除该文件,下一次提交将缺少该文件),但如果您返回到 [=52] 的提交,它会产生问题=] 是否有该文件,因为 git 会发现它需要创建该文件——您正在从一个文件不存在的提交(最近的一个)转移到一个提交它确实存在于其中(一个旧文件)——并且可能会抱怨该文件的内容将被破坏。或者,即使这部分有效,如果您随后回到最新版本,远离旧版本,git 将比较旧版本和新版本并查看该文件已被删除......并且将移除 mllib/pom.xml.

重新编辑,2016 年 10 月 20 日:使用 git update-index --skip-worktree,而不是 git update-index --assume-unchanged。这在索引中设置了一个更强大的位。有关详细信息,请参阅 Git - Difference Between 'assume-unchanged' and 'skip-worktree'. Edit: as , you can use git update-index --assume-unchanged to make git not even look at the file for changes, rather than using git rm --cached to take it out of the index (see this answer)。就目前而言,这也很好,您可能需要在任何 other/new 克隆上再做一次(或让您的所有同事自己做)。

(您可以通过使用 git show <oldrev>:mllib/pom.xml > mllib/pom.xml 将文件转储到标准输出并重定向标准输出以重新创建文件来从后者恢复。)


1Inconceivable!

(更严重的是,每个人都会犯这个错误。可能 gitignore 是错误的名称,如果更笨的话,git-screen-away-untracked 之类的名称可能会更好。但是,在 [=11 中列出一个文件=] 还有其他副作用:具体来说,在某些情况下 permits Git to clobber such files。)