Git为什么不忽略我指定的文件? [英] Why doesn't Git ignore my specified file?

查看:74
本文介绍了Git为什么不忽略我指定的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.gitignore中添加了以下行:

sites/default/settings.php

但是当我键入git status时,它将文件显示为未暂存的文件.

but when I type git status it shows the file as unstaged file.

出什么问题了?所有其他模式都可以正常工作.

What's the problem? All other patterns work well.

推荐答案

请确保您的.gitignore在工作目录的根目录中,并在该目录中运行git status copy 状态输出中文件的路径,并将其粘贴到.gitignore.

Make sure that your .gitignore is in the root of the working directory, and in that directory run git status and copy the path to the file from the status output and paste it into the .gitignore.

如果这不起作用,则可能是Git已经跟踪了您的文件.您可以通过git status的输出来确认.如果文件未在未跟踪的文件" 部分中列出,则Git已对其进行了跟踪,它将忽略.gitignore文件中的规则.

If that doesn’t work, then it’s likely that your file is already tracked by Git. You can confirm this through the output of git status. If the file is not listed in the "Untracked files" section, then it is already tracked by Git and it will ignore the rule from the .gitignore file.

忽略Git中文件的原因是,它们不会被添加到存储库中.如果您以前添加了要忽略的文件,则Git将对其进行跟踪,并且将跳过与该文件匹配的忽略规则. Git这样做是因为文件已经是存储库的一部分.

The reason to ignore files in Git is so that they won't be added to the repository. If you previously added a file you want to be ignored, then it will be tracked by Git and the ignore rules matching it will be skipped. Git does this since the file is already part of the repository.

为了实际上忽略文件,您必须取消跟踪并将其从存储库中删除.您可以使用git rm --cached sites/default/settings.php来做到这一点.这样可以从存储库中删除文件,而无需实际删除文件(--cached就是这样做的).提交更改后,该文件将从存储库中删除,并忽略它应能正常工作.

In order to actually ignore the file, you have to untrack it and remove it from the repository. You can do that by using git rm --cached sites/default/settings.php. This removes the file from the repository without physically deleting the file (that’s what the --cached does). After committing that change, the file will be removed from the repository, and ignoring it should work properly.

这篇关于Git为什么不忽略我指定的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆