git gitignore 不会忽略文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24410208/
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
gitignore does not ignore folder
提问by Lajos Arpad
In the root of my project I have a foo
folder. Inside the foo
folder I have a bar
folder. I would like to ignore all changes to all files inside my bar
folder. I have this in my gitignore
:
在我的项目的根目录中,我有一个foo
文件夹。在foo
文件夹内我有一个bar
文件夹。我想忽略对我bar
文件夹中所有文件的所有更改。我有这个gitignore
:
/foo/bar
The folder is checked: it exists and it has the files to be ignored. gitignore
is commit
ted. However, I have a file where I make a moification and is inside my bar
folder. When I type
检查文件夹:它存在并且它有要忽略的文件。gitignore
是commit
ted。但是,我有一个文件,我在其中进行了修改,并且在我的bar
文件夹中。当我打字
git status
inside my git bash
I see the file which should have been ignored. What could be the reason and how can I successfully ignore all files inside my bar folder?
在我的里面,我git bash
看到了应该被忽略的文件。可能是什么原因,如何成功忽略 bar 文件夹中的所有文件?
Note, that the files were previously ignored with the same line, but I had to temporarily remove that line to commit
something on the server. After the commit
, I put back the line into the gitignore
. This was a while ago, but now I have observed that the files will be in git status
. I would expect to be able to modify the ignored files without they appearing in the git status
.
请注意,这些文件以前在同一行中被忽略,但我不得不暂时将该行删除到commit
服务器上的某些内容。之后commit
,我把线放回了gitignore
。这是前一段时间,但现在我观察到文件将在git status
. 我希望能够修改被忽略的文件,而它们不会出现在git status
.
回答by Reck
I'm guessing this folder has been checked into git before?
我猜这个文件夹之前已经被签入过git?
Run git rm -r --cached <folder>
and check again.
运行git rm -r --cached <folder>
并再次检查。
回答by sikanderBabwani
For me, the accepted answer was part of the solution, not the entire solution. Maybe, the other steps that I'm about to post were obvious, but I missed them first. Here's the steps I took to ensure my .gitignore
file ignored the folder I wanted it to ignore:
对我来说,接受的答案是解决方案的一部分,而不是整个解决方案。也许,我即将发布的其他步骤很明显,但我首先错过了它们。以下是我为确保我的.gitignore
文件忽略了我希望它忽略的文件夹而采取的步骤:
- Commit any changes that you need to fix/change.
- Run this command:
git rm -r --cached .
(which removes everything from the git index in order to refresh your git repository) - Then run this command:
git add .
(to add everything back to the repo) - Finally, commit these changes using
git commit -m ".gitignore Fixed"
- 提交您需要修复/更改的任何更改。
- 运行此命令:(
git rm -r --cached .
它从 git 索引中删除所有内容以刷新您的 git 存储库) - 然后运行此命令:(
git add .
将所有内容添加回 repo) - 最后,使用提交这些更改
git commit -m ".gitignore Fixed"
You can find the link to the article from where I found the solution here.
您可以在此处找到我在此处找到解决方案的文章链接。
回答by Mark Hymanson
I was having this issue and I realized git was actually ignoring the files/folders correctly, but my code editor (Visual Studio Code) was just buggy and not "greying them out" properly in the UI sidebar. I restarted VSCode and they were greyed out as expected.
我遇到了这个问题,我意识到 git 实际上正确地忽略了文件/文件夹,但是我的代码编辑器(Visual Studio Code)只是有问题,并且没有在 UI 侧栏中正确地“将它们变灰”。我重新启动了 VSCode,它们按预期变灰。
回答by learnsomemore
As an addition to the accepted answer
作为已接受答案的补充
git rm -r --cached /foo/bar/
git status
When i do that, the terminal shows a bunch of rm
for files in that directory. So to prevent another commit that might unnecessarily affect remote, I i did:
当我这样做时,终端会显示rm
该目录中的一堆文件。因此,为了防止可能不必要地影响远程的另一个提交,我做了:
git reset HEAD *
git status
After that, it said nothing to commit and when i modify files inside /foo/bar/
and do a git status
i still get nothing to commit
.
在那之后,它什么也没说提交,当我修改里面的文件/foo/bar/
并做一个git status
我仍然得到nothing to commit
.