简体   繁体   English

如何在Git源代码管理下用子文件夹制作一个单独的Eclipse项目

[英]How to make a separate Eclipse project out of a subfolder under Git source-control

I have a website project with the main files in a root folder and an admin tool in a subfolder. 我有一个网站项目,其主文件位于根文件夹中,而管理工具位于子文件夹中。 The admin tool includes a few classes from the root folder. 管理工具包括根文件夹中的一些类。

I would like to keep separate version control over the 'site' (root) and the 'admin tool' (subfolder) with Git. 我想使用Git对“站点”(根)和“管理工具”(子文件夹)进行单独的版本控制。 With Git ignoring the subfolder in the root and a separate repository in the subfolder this works fine. 使用Git忽略根目录中的子文件夹,并忽略子文件夹中的单独存储库,即可正常工作。

However, I like using Eclipse (4.4.2) as my Git GUI but Eclipse manages this on a project basis and when I try to create a new project in Eclipse of the subfolder it complains that this is not allowed because that folder is already part of another project (root folder). 但是,我喜欢使用Eclipse(4.4.2)作为我的Git GUI,但是Eclipse是基​​于项目管理的,当我尝试在子文件夹的Eclipse中创建新项目时,它抱怨不允许这样做,因为该文件夹已经是一部分另一个项目(根文件夹)。

Is there a way, just like in Git, to make Eclipse ignore a subfolder in a project so that I can create a separate project of the subfolder? 就像在Git中一样,是否有办法使Eclipse忽略项目中的子文件夹,以便我可以创建子文件夹的单独项目? Or should I approach the whole thing differently? 还是我应该以不同的方式对待整个事情?

The right and convenient way: 正确便捷的方法:

You can make a submodule out of that folder with git-subtree command. 您可以使用git-subtree命令从该文件夹中创建一个子模块。

After making a submodule you will be able to manage your project as two git repos, one nested in another. 制作一个子模块后,您将能够以两个git repos(一个嵌套于另一个)的方式管理您的项目。 Eclipse must understand this structure and allow to make separate commits. Eclipse必须了解此结构并允许进行单独的提交。 You also will be able to have different remote repositories for each of your sub-projects. 您还将能够为每个子项目拥有不同的远程存储库。

Here's a good manual on git-subtree: Detach (move) subdirectory into separate Git repository 这是有关git-subtree的好手册: 将子目录分离(移动)到单独的Git存储库中

The not so right and convenient way. 不太正确和便捷的方法。

You can add that subfolder in .gitignore of the main folder and then initiate a new repo inside it. 您可以将该子文件夹添加到主文件夹的.gitignore中,然后在其中启动新的存储库。

Eclipse unfortunately doesn't visually indicate separate changes when using submodules and managing the root folder and the subfolder as separate projects in Eclipse helps me to keeps things separately organized. 不幸的是,当使用子模块并管理根文件夹和子文件夹时,Eclipse不会在视觉上指示单独的更改,因为Eclipse中的单独项目有助于我保持事物的独立组织。 That is why I chose to .gitignore the subfolder in the rootfolder and create a separate repo in the subfolder. 这就是为什么我选择在根文件夹中.gitignore子文件夹并在子文件夹中创建单独的存储库。

Bringing this to Eclipse was a hassle because there are some known bugs in Eclipse that (ironically) make EGit ignore the .gitignore file. 将此问题带到Eclipse很麻烦,因为Eclipse中有一些已知的错误 (具有讽刺意味的是)使EGit忽略了.gitignore文件。 This makes Eclipse insist on committing the .git folder it finds in the subfolder as a submodule, even though it should be ignoring it. 这使Eclipse坚持将在子文件夹中找到的.git文件夹作为子模块提交,即使它应该忽略它。

I now have everything setup using the following steps: 我现在使用以下步骤进行所有设置:

  1. creating a symbolic link (Windows) of the subfolder to a folder next to the root folder. 在子文件夹中创建到根文件夹旁边的文件夹的符号链接 (Windows)。 This helps bypass the Eclipse restriction of creating projects in subfolders of folders that are themselves already a project. 这有助于绕过Eclipse的限制,即在本身已经是项目的文件夹的子文件夹中创建项目。
  2. creating the repos before adding the projects to Eclipse, otherwise Eclipse will not use the .gitignore file. 在将项目添加到Eclipse 之前创建回购协议 ,否则Eclipse将不会使用.gitignore文件。 Even if you use EGit to create them. 即使您使用EGit创建它们。
  3. Writing the .gitignore file with Unix style line breaks , using a forward slash to ignore the /subfolder. 使用Unix样式换行符编写.gitignore文件,使用正斜杠忽略/ subfolder。 Git itself handles other formats fine but Eclipse won't use the .gitignore correctly if it isn't exactly written that way. Git本身可以很好地处理其他格式,但是Eclipse不会正确使用.gitignore,如果它不是用这种方式编写的。
  4. To clean things up; 清理 hiding the subfolder from the 'site' (root) project by adding a Recourse Filter on the project that hides the subfolder from the project tree. 通过在项目上添加一个资源筛选器以从项目树中隐藏子文件夹,从而从“站点”(根)项目中隐藏子文件夹。 This way, the file from the 'admin tool' subfolder can only be accessed in Eclipse through its own project. 这样,只能在Eclipse中通过其自己的项目访问“管理工具”子文件夹中的文件。

I now have two separate projects in Eclipse with each their own Git repository, even though one is technically a subfolder of the other. 我现在在Eclipse中有两个单独的项目,每个项目都有他们自己的Git存储库,即使从技术上来说,一个项目是另一个项目的子文件夹。 I wrote a details tutorial about this in this post about managing a subfolder as a project in Eclipse, Git and FTP . 我在这篇文章中写了一篇关于此的详细教程,内容涉及在Eclipse,Git和FTP中将子文件夹作为项目进行管理

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM