仓库内的 Git 仓库

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

Git repo inside repo

gitgit-submodules

提问by null

I want to clone https://github.com/getyouridx/pychargifyinto my django project, and will need to pull updates from time-to-time.

我想将https://github.com/getyouridx/pychargify克隆到我的 django 项目中,并且需要不时提取更新。

Just for clarification, could I simply write a gitignore from the root directory of the django project e.g. .gitignore: pychargify/.gitor are there other pitfalls I should be aware of?

只是为了澄清,我可以简单地从 django 项目的根目录中编写一个 gitignore 例如,.gitignore: pychargify/.git或者还有其他我应该注意的陷阱吗?

回答by Trott

To have one git repo "inside" another, look at git submodules: http://git-scm.com/book/en/Git-Tools-Submodules

要让一个 git repo“在”另一个“内部”,请查看 git 子模块:http: //git-scm.com/book/en/Git-Tools-Submodules

By making pychargify a submodule of your django project, specific revisions of your django project can be associated with specific revisions of your pychargify project. That can be really useful.

通过使 pychargify 成为 django 项目的子模块,django 项目的特定修订版可以与 pychargify 项目的特定修订版相关联。这真的很有用。

I'm not sure exactly what the dangers are of the approach you describe, but it doesn't pass the smell test for me. I would recommend using the Git feature (submodules) that is designed specifically for this type of thing.

我不确定你描述的方法到底有什么危险,但它没有通过我的气味测试。我建议使用专为此类事物设计的 Git 功能(子模块)。

回答by Andrew Marshall

Git has a feature for having a repository within another: submodules.

Git 有一个特性,可以在另一个存储库中拥有一个存储库:submodules

git submodule add https://github.com/getyouridx/pychargify.git

Be sure to read the entire documentation on submodules, as there as a few quirks involved with using them, and additional steps that need to be taken when doing a fresh clone of your own repository to initialize the submodules.

请务必阅读有关子模块的整个文档,因为使用它们会涉及一些怪癖,以及在对自己的存储库进行全新克隆以初始化子模块时需要采取的其他步骤。

Also note that all submodule commands must be done in the root directory of your repository.

另请注意,所有子模块命令都必须在存储库的根目录中完成。

回答by manojlds

Git automatically ignores and wouldn't even allow you to add any file / folder named .git. So you can just add a repo within your repo and work on it. You might have to ignore the inner repo folder pychargifyhowever.

Git 会自动忽略甚至不允许您添加任何名为.git. 所以你可以在你的 repo 中添加一个 repo 并处理它。但是,您可能必须忽略内部 repo 文件夹pychargify

Submodules are needed when you want to share the repo with others who will be cloning it etc. If you are just looking at cloning the inner repo and working on your local repo with no one else involved or you don't want to have the repo elsewhere as well, you don't really need submodules.

当您想与将要克隆它的其他人共享 repo 时,需要子模块。在其他地方,您也不需要子模块。

回答by Wtower

Git subtrees

Git 子树

Git submodulesis a common way, gaining much ground since introduced, in order to deal with the situation where one may want to add a project (repo) within another project (repo), as the other answers have correctly described.

Git子模块是一种常见的方式,自引入以来获得了很大的发展,以处理可能想要在另一个项目 (repo) 中添加一个项目 (repo) 的情况,正如其他答案所正确描述的那样。

Nevertheless, one could argue that the submodules way is not the only way and occassionaly not the proper way to go, depending on established workflows, for several reasons which I am not going to analyze and which are briefly mentioned in several pages such as thisand this. The most important is arguably this:

然而,有人可能会争辩说,子模块方式不是唯一的方式,有时也不是正确的方式,具体取决于已建立的工作流程,原因有几个我不打算分析,并且在几个页面中简要提到,例如this这个。最重要的可以说是:

When Git drops into conflict resolution mode, it still doesn't update the submodule pointers – which means that when you commit the merge after resolving conflicts, you run into the same problem ...: if you forgot to run git submodule update, you've just reverted any submodule commits the branch you merged in might have made.

当 Git 进入冲突解决模式时,它仍然不会更新子模块指针——这意味着当你在解决冲突后提交合并时,你会遇到同样的问题......:如果你忘记运行 git submodule update,你'刚刚恢复了您合并的分支可能已经提交的任何子模块提交。

Of course in a perfect working flow this would never happen.

当然,在完美的工作流程中,这永远不会发生。

Another important reason is that the popular PyCharm IDE (when this is written; there is a very old issuefor that) and possibly others as well do not fully implement git submodules and the coder will lose among others the nifty funtionality of the IDE displaying all changed lines in the submodule.

另一个重要的原因是流行的 PyCharm IDE(编写此代码时;有一个非常古老的问题)和其他可能也没有完全实现 git 子模块,编码器将失去 IDE 的漂亮功能,其中显示所有更改了子模块中的行。

Therefore an alternative way to deal with this issue is to use subtrees. Notice that subtrees and subtree mergingis not exactly the same thing, but this is again another matter. The excellent Progit bookin its 2nd edition briefly covers the latter, but not a single reference for the former.

因此,处理此问题的另一种方法是使用子树。请注意,子树和子树合并并不完全相同,但这又是另一回事。优秀的Progit 书在其第二版中简要介绍了后者,但没有单独参考前者。

So in a practical example, in order to deal with the situation under concern, let assume a subprojectto be consumed in a project:

所以在一个实际的例子中,为了处理所关注的情况,假设 asubproject在 a 中被消耗project

$ git remote add subproject_remote (url)
# subproject_remote is the new branch name and (url) where to get it from, it could be a path to a local git repo

$ git subtree add —-prefix=subproject/ subproject_remote master
# the prefix is the name of the directory to place the subproject

$ git commit -am "Added subproject"
# possibly commit this along with any changes

If the subproject changes, to pull them into project:

如果子项目发生变化,将它们拉入project

git subtree pull —prefix=subproject subproject_remote master

...or the opposite (if changes are made in the subprojectinside the project):

...或相反(如果在subproject内部进行了更改project):

git subtree push —prefix=subproject subproject_remote new_branch

An analytical but rather clattered tutorial in this link.

链接中的分析性但相当混乱的教程。

This functionality has got some drawbacks as well, for instance a lot of people find the working flow cumbersome and more complicated, but again this depends on the particular established workflows.

这个功能也有一些缺点,例如很多人发现工作流程繁琐和复杂,但这同样取决于特定的既定工作流程。

回答by Pat Niemeyer

Adding a folder containing a git project within another git project works as you might expect: there is no direct interaction between them and you can commit changes independently by working in one directory or the other. You can either have the "parent" project ignore the inner folder entirely or you can commit selected files from the child folder as if they were part of the parent project.

在另一个 git 项目中添加一个包含 git 项目的文件夹,就像您预期的那样:它们之间没有直接交互,您可以通过在一个目录或另一个目录中工作来独立提交更改。您可以让“父”项目完全忽略内部文件夹,也可以提交子文件夹中的选定文件,就好像它们是父项目的一部分一样。

If you do the latter then when you modify a file it will be seen as modified by both git projects and you can manage those changes independently (commit the changes) by simply working in one directory or the other.

如果你做后者,那么当你修改一个文件时,它会被两个 git 项目视为修改,你可以通过简单地在一个目录或另一个目录中工作来独立管理这些更改(提交更改)。

I'm assuming you are using the command line tools here, although I believe XCode 7 may understand the situation and display change annotations on the files as long as the files are seen as modified by one or both of the git repositories.

我假设您在这里使用命令行工具,但我相信 XCode 7 可能会理解这种情况并在文件上显示更改注释,只要文件被视为由一个或两个 git 存储库修改。

I find the procedure above to be simpler than dealing with submodules but others have commented on how to use those so you may want to compare.

我发现上面的过程比处理子模块更简单,但其他人已经评论了如何使用它们,因此您可能想要进行比较。