无法取消忽略 Git 存储库中先前忽略的文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11126292/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:58:58  来源:igfitidea点击:

Trouble un-ignoring previously ignored files in Git repo

gitgitignore

提问by doub1eHyman

I have a site that has a /sites/default/files/directory where user content is typically kept. Figuring that I don't need to be tracking this stuff, I added /sites/default/files/ to my .gitignore file.

我有一个站点,其中包含一个/sites/default/files/通常保存用户内容的目录。考虑到我不需要跟踪这些东西,我将 /sites/default/files/ 添加到我的 .gitignore 文件中。

Then I discovered that I also wanted to keep some mostly-permanent uploads in that directory as well. Now it makes more sense to track everything in /sites/default/files/ and exclude undesirable subdirectories, instead of doing it the other way around.

然后我发现我还想在该目录中保留一些大部分永久上传。现在跟踪 /sites/default/files/ 中的所有内容并排除不需要的子目录更有意义,而不是相反。

The problem is that Git won't stop ignoring that directory. I have removed the lines from .gitignore that specify this directory and it won't track it. I have the option to use git add /sites/default/files/ -fto force git to track the directory, but I've done that before and it seems messy.

问题是 Git 不会停止忽略该目录。我已经从 .gitignore 中删除了指定这个目录的行,它不会跟踪它。我可以选择使用git add /sites/default/files/ -f强制 git 跟踪目录,但我以前这样做过,而且看起来很乱。

If .gitignore no longer specifies that directory as ignored, why would I have to force Git to start tracking those files?

如果 .gitignore 不再指定该目录被忽略,为什么我必须强制 Git 开始跟踪这些文件?

采纳答案by doub1eHyman

For posterity: the answer to my problem was "just look closer".

对于后代:我的问题的答案是“仔细观察”。

@twalberg pointed out, correctly, that there might be a rule I was overlooking in the .gitignore file. The cascading manner in which the .gitignore rules are applied make it easy to exclude files more broadly than intended.

@twalberg 正确地指出,我可能在 .gitignore 文件中忽略了一条规则。应用 .gitignore 规则的级联方式可以轻松地比预期更广泛地排除文件。

As far as I can tell, the assumptions I was making about how git works were correct. Similarly, my case doesn't seem to support (or disprove) @J-16 SDiZ 's comments on what role the assume-unchangedbit may play.

据我所知,我对 git 如何工作的假设是正确的。同样,我的案例似乎不支持(或反驳)@J-16 SDiZ 关于该assume-unchanged位可能扮演什么角色的评论。

回答by J-16 SDiZ

You know how to do it already. Yes, git add -fis the way.

你已经知道怎么做了。是的,git add -f就是这样。

If you ask why... this is because git set a assume-unchangedbit internally. This bit make git think the file is not modified, so git addwon't add it.

如果你问为什么......这是因为 git 在assume-unchanged内部设置了一些。这一点让 git 认为文件没有被修改,所以git add不会添加它。

If you want to mess with the implementation details, you can use git update-index --no-assume-unchanged <file>command.

如果你想弄乱实现细节,你可以使用git update-index --no-assume-unchanged <file>命令。

回答by hdgarrood

I don't think J-16 is correct. From http://www.kernel.org/pub/software/scm/git/docs/git-add.html:

我不认为 J-16 是正确的。从http://www.kernel.org/pub/software/scm/git/docs/git-add.html

The git add command will not add ignored files by default. If any ignored files were explicitly specified on the command line, git add will fail with a list of ignored files. Ignored files reached by directory recursion or filename globbing performed by Git (quote your globs before the shell) will be silently ignored. The git add command can be used to add ignored files with the -f (force) option.

git add 命令默认不会添加被忽略的文件。如果在命令行上明确指定了任何被忽略的文件, git add 将失败并显示被忽略文件的列表。通过目录递归或由 Git 执行的文件名通配符(在 shell 之前引用您的通配符)到达的忽略文件将被静默忽略。git add 命令可用于通过 -f(强制)选项添加被忽略的文件。

git add -fseems to only be for adding a file that doesexist in an ignore file somewhere.

git add -f似乎只是为了添加一个确实存在于某个忽略文件中的文件。

Perhaps there is another .gitignore file in /sites or /sites/default which is also telling Git to ignore this dir, or maybe it's been set in .git/info/exclude?

也许 /sites 或 /sites/default 中还有另一个 .gitignore 文件,它也告诉 Git 忽略这个目录,或者它可能已在 .git/info/exclude 中设置?

回答by Al Crowley

We had a very similar problem in our Drupal sites directory. The issue is that the Drupal distributionhas a .gitignore file in the higher level drupal directory that matches anything in drupal/sites/files/*

我们在 Drupal 站点目录中遇到了非常相似的问题。问题是Drupal 发行版在更高级别的 drupal 目录中有一个 .gitignore 文件,该文件与 drupal/sites/files/* 中的任何内容相匹配