Git - 如何取消忽略文件

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

Git - how to unignore a file

gitgitignore

提问by Lord Zsolt

Long story short, I accidentally added a file to .gitignore, which I committed and pushed.

长话短说,我不小心向 .gitignore 添加了一个文件,我提交并推送了该文件。

Now I removed the file from .gitignore, but it's still not visible.

现在我从 .gitignore 中删除了该文件,但它仍然不可见。

What else do I need to do?

我还需要做什么?

回答by Lord Zsolt

I'm stupid, it was left in global gitignore, which is why it wasn't showing.

我很愚蠢,它被留在了全局 gitignore 中,这就是它没有显示的原因。

回答by Jay Zelenkov

You might want to recover that file from repository and commit it again:

您可能希望从存储库中恢复该文件并再次提交:

git checkout 35fbd83 -- path/to/your/file

35fbd83is SHA of a commit where that ignored file was still present.

35fbd83是被忽略的文件仍然存在的提交的 SHA。

git add path/to/your/file
git commit -m 'missing file is back'

After a file is added to .gitignoreits not tracked anymore. If you accidentally or purposely remove that file while it's ignored, git won't complain and that file silently disappears from your file system. Commands like git reset --hardand git checkoutcould lead to a situation where ignored files are removed from your working directory.

将文件添加到.gitignore不再跟踪后。如果您在忽略该文件时意外或故意删除了该文件,git 不会抱怨并且该文件会从您的文件系统中默默消失。像git reset --hardgit checkout这样的命令可能会导致从工作目录中删除被忽略的文件的情况。