git 忽略git中的子目录的问题

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

Issue with ignoring subdirectory in git

gitgitignore

提问by Ittai

I have a maven multi-modules project which has a depth of around 5 levels.
I'm now moving to git and I see that a lot of the content of the targetfolder is captured by git as "Unstaged Changes".
I've googled a lot and searched SO but couldn't find an answer as to how to get git to ignore the entiredirectory tree of the targetfolder.
My .gitignore, which resides at the root of the project, looks like this:

我有一个 Maven 多模块项目,它的深度约为 5 个级别。
我现在正在转向 git,我看到该target文件夹的很多内容都被 git 捕获为“Unstaged Changes”。
我在谷歌上搜索了很多并搜索了 SO 但找不到关于如何让 git 忽略文件夹的整个目录树的答案target
My.gitignore位于项目的根目录,如下所示:

.project  
.classpath
.settings/
target/

The strange thing is that the .classfiles, which reside somewhere under the targettree, do get ignored by the above definitions but files like MANIFEST.MF, pom.propertiesor my own application properties files which are located somewhere in the targetdirectory tree are not ignored.
I've read Git Ignores and Maven targetsbut that solution does not seem to work for me.
I can also say that when I try to add target/*rule it doesn't change anything and when I replace target/with the target/*then the .classfiles appear as unstaged as well.
I'd really appreciate some guidance as it seems very unlikely to me that the hordes of people using git and maven haven't already resolved similar issues. Thanks.

奇怪的是,.class该下的某处存在的文件,target树,确实会被上述定义忽略,但文件喜欢MANIFEST.MFpom.properties或位于什么地方我自己的应用程序属性文件target目录树不忽略。
我已经阅读了Git Ignores 和 Maven 目标,但该解决方案似乎对我不起作用。
我也可以说,当我尝试添加target/*规则,它不会改变任何东西,当我更换target/target/*那么.class文件显示为不分级为好。
我真的很感激一些指导,因为在我看来,使用 git 和 maven 的成群结队的人还没有解决类似的问题的可能性很小。谢谢。

回答by KL-7

One of the reasons might be that you already committed that files to your repository. In that case adding them to .gitignoredoesn't prevent git from tracking these files. To fix that you should first remove them from repository like that:

原因之一可能是您已经将这些文件提交到您的存储库。在这种情况下,将它们添加到.gitignore不会阻止 git 跟踪这些文件。要解决这个问题,您应该首先像这样从存储库中删除它们:

git rm -r target/

(If your project has modules, you need to include the path down to the relevant module's target:
git rm -r parent_project/module_name/target/)

(如果你的项目有模块,则需要包括路径到相关的模块的目标:
git rm -r parent_project/module_name/target/

If you want to remove directory from repository but not from disk you can use --cachedoption:

如果您想从存储库中删除目录而不是从磁盘中删除,您可以使用--cached选项:

git rm -r --cached target/

In any case you should then commit that deletion to the repository (you can do it in the same commit with adding target/to .gitignoreif you wish).

在任何情况下,你应该然后提交该删除的存储库(你可以做到这一点在同与添加提交target/.gitignore,如果你愿意的话)。

回答by Manish Shrestha

target
target/*

Add these lines in the gitignore to ignore the target file and all the files inside target. This should not track that folder and its content anymore.

在 gitignore 中添加这些行以忽略目标文件和目标中的所有文件。这不应再跟踪该文件夹及其内容。

回答by istibekesi

Alternatively, using tortoisegiton folder target simply choose "Delete and add to ignore list".

或者,在文件夹目标上使用tortoisegit只需选择“删除并添加到忽略列表”。