git status 显示 .gitignore 中列出的文件的“更改未暂存以进行提交”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28101020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
git status is showing "Changes not staged for commit" for files listed in .gitignore?
提问by javadba
Let is look at the .gitignore file - to which I added mllib/pom.xml and pom.xml and even .gitignore (which should not be necessary - so something is amiss..):
让我们看看 .gitignore 文件 - 我向其中添加了 mllib/pom.xml 和 pom.xml 甚至 .gitignore (这应该不是必需的 - 所以有些不对劲......):
$head .gitignore
.gitignore
mllib/pom.xml
pom.xml
So then let's see what files git wants to add:
那么接下来让我们看看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:
UPDATEThere are two comments about not "ignoring the .gitignore". But after removing the .gitignore again we get this:
更新有两条关于不“忽略 .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
So (a) .gitignore is showing up and (b) the specific file that really is important not to add to a commit - mllib/pom.xml - alsoshows up.
所以 (a) .gitignore 出现了,(b) 不添加到提交中非常重要的特定文件 - mllib/pom.xml -也出现了。
回答by torek
The .gitignore
file doesn't mean what you think it means.1
该.gitignore
文件并不意味着您认为它意味着什么。1
In particular, once you have made a file "known" to git, in that it's in the index, adding that file's name to .gitignore
has no effect. Git already tracks the file, and it will continue to track it.
特别是,一旦您让 git “已知”了一个文件,因为它在索引中,添加该文件的名称.gitignore
就没有任何效果。Git 已经跟踪该文件,它将继续跟踪它。
In essence, the .gitignore
file is not actually a list of files to ignore. Instead, when git comes across a case of an "untracked" file and is about to report it to you, the .gitignore
contents are a list of names it should suppress.
从本质上讲,该.gitignore
文件实际上并不是要忽略的文件列表。相反,当 git 遇到“未跟踪”文件的情况并准备向您报告时,.gitignore
内容是它应该抑制的名称列表。
For the .gitignore
file itself you probably just want to git add
the changes and commit them, since as a general rule, anyone cloning your repository probably wants to ignore the same set of untracked files, so .gitignore
should be tracked and version-controlled.
对于.gitignore
文件本身,您可能只想git add
更改并提交它们,因为作为一般规则,克隆您的存储库的任何人都可能希望忽略同一组未跟踪的文件,因此.gitignore
应该进行跟踪和版本控制。
For the XML file, however, it may be "generated content" that you don't want version-controlled, but you still want to keep it in the work-tree. This is a bit of a problem, because git is already tracking it and will insist on continuing to version-control the file.
然而,对于 XML 文件,它可能是您不希望版本控制的“生成内容”,但您仍然希望将其保留在工作树中。这有点问题,因为 git 已经在跟踪它并且会坚持继续对文件进行版本控制。
What you can do at this point is remove it from git's index, but not from the work-tree:
此时您可以做的是将它从 git 的索引中删除,而不是从工作树中删除:
git rm --cached mllib/pom.xml
That's fine as far as it goes (git removes the file from the index and the next commit will lack that file), but it creates problems if you ever go back to a commit that doeshave the file, because git will see that it needs to create the file—you're moving from a commit in which the file does not exist (a recent one) to a commit in which it does exist (an old one)—and may complain that the contents of that file will be clobbered. Or, even if this part works, if you then move back to a recent version, getting away from the old version, git will compare the old and new versions and see that the file has been removed ... and will removemllib/pom.xml
.
就目前而言这很好(git 从索引中删除文件,下一次提交将缺少该文件),但是如果您返回到确实有该文件的提交,则会产生问题,因为 git 会看到它需要创建文件——你正在从一个文件不存在的提交(最近的一个)转移到一个它确实存在的提交(一个旧的)——并且可能会抱怨该文件的内容将被破坏. 或者,即使这部分工作正常,如果您然后移回最新版本,远离旧版本,git 将比较旧版本和新版本,并查看文件已被删除......并将删除mllib/pom.xml
.
Re-edit, 20 Oct 2016: Use git update-index --skip-worktree
, not git update-index --assume-unchanged
. This sets a more powerful bit in the index. See Git - Difference Between 'assume-unchanged' and 'skip-worktree'. Edit: as Schwern noted in a comment below, you can use git update-index --assume-unchanged
to make git not even lookat the file for changes, rather than using git rm --cached
to take it out of the index (see this answerfor details). This is also fine as far as it goes, you just may have to do it again (or have all your co-workers do it themselves) on any other/new clones.
重新编辑,2016 年 10 月 20 日:使用git update-index --skip-worktree
,而不是git update-index --assume-unchanged
. 这在索引中设置了一个更强大的位。请参阅Git - 'assume-unchanged' 和 'skip-worktree' 之间的区别。 编辑:正如Schwern 在下面的评论中指出的那样,您可以使用git update-index --assume-unchanged
git 甚至不查看文件的更改,而不是使用git rm --cached
将其从索引中取出(有关详细信息,请参阅此答案)。就目前而言,这也很好,您可能只需要在任何其他/新克隆上再做一次(或让您的所有同事自己做)。
(You can recover from the latter by using git show <oldrev>:mllib/pom.xml > mllib/pom.xml
to dump the file to standard output, and redirect standard output to re-create the file.)
(您可以通过使用git show <oldrev>:mllib/pom.xml > mllib/pom.xml
将文件转储到标准输出并重定向标准输出以重新创建文件来从后者中恢复。)
(More seriously, everyone makes this mistake. Probably gitignore
was the wrong name, and something like git-screen-away-untracked
might have been better, if klunkier. However, listing a file in .gitignore
has other side effects as well: specifically, it permits Git to clobber such filesin some cases.)
(更严重的是,每个人都会犯这个错误。可能gitignore
是错误的名称,git-screen-away-untracked
如果更笨拙的话,类似的东西可能会更好。但是,列出文件.gitignore
也有其他副作用:特别是,它允许 Git在某些情况。)