嵌套的 Git 存储库?

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

Nested Git repositories?

git

提问by Jeremy Raymond

Can I nest Git repositories? I have:

我可以嵌套 Git 存储库吗?我有:

 /project_root/
 /project_root/my_project
 /project_root/third_party_git_repository_used_by_my_project

Does it make sense to git init/addthe /project_rootto ease management of everything locally or do I have to manage my_projectand the 3rd party one separately?

它是否有意义git init/add/project_root,以缓解当地的一切管理或做我必须管理my_project并单独的第三方呢?

采纳答案by Greg Hewgill

You may be looking for the Git feature called submodules. This feature helps you manage dependent repositories that are nested inside your main repository.

您可能正在寻找名为submodules的 Git 功能。此功能可帮助您管理嵌套在主存储库中的依赖存储库。

回答by Igor Zevaka

Place your third party libraries in a separate repository and use submodules to associate them with the main project. Here is a walk-through:

将第三方库放在单独的存储库中,并使用子模块将它们与主项目相关联。这是一个演练:

http://git-scm.com/book/en/Git-Tools-Submodules

http://git-scm.com/book/en/Git-Tools-Submodules

In deciding how to segment a repo I would usually decide based on how often I would modify them. If it is a third-party library and only changes you are making to it is upgrading to a newer version then you should definitely separate it from the main project.

在决定如何分割回购时,我通常会根据我修改它们的频率来决定。如果它是第三方库,并且您对其所做的更改只是升级到更新版本,那么您绝对应该将其与主项目分开。

回答by Phil

Just for completeness:

只是为了完整性:

There is another solution, I would recommend: subtree merging.

还有另一种解决方案,我会推荐:subtree merging

In contrast to submodules, it's easier to maintain. You would create each repository the normal way. While in your main repository, you want to merge the master (or any other branch) of another repository in a directory of your main directory.

与子模块相比,它更容易维护。您将以正常方式创建每个存储库。在主存储库中,您希望将另一个存储库的主库(或任何其他分支)合并到主目录的目录中。

$ git remote add -f OtherRepository /path/to/that/repo
$ git merge -s ours --no-commit OtherRepository/master
$ git read-tree --prefix=AnyDirectoryToPutItIn/ -u OtherRepository/master
$ git commit -m "Merge OtherRepository project as our subdirectory"`

Then, in order to pull the other repository into your directory (to update it), use the subtree merge strategy:

然后,为了将另一个存储库拉入您的目录(以更新它),请使用子树合并策略:

$ git pull -s subtree OtherRepository master

I'm using this method for years now, it works :-)

我已经使用这种方法多年了,它有效:-)

More about this way including comparing it with sub modules may be found in this git howto doc.

可以在这个 git howto doc 中找到更多关于这种方式的信息,包括将它与子模块进行比较。

回答by mikkelbreum

You could add

你可以添加

/project_root/third_party_git_repository_used_by_my_project

to

/project_root/.gitignore

that should prevent the nested repo to be included in the parent repo, and you can work with them independently.

这应该可以防止嵌套存储库包含在父存储库中,并且您可以独立使用它们。

But: If a user runs git clean -dfx in the parent repo, it will remove the ignored nested repo. Another way is to symlink the folder and ignore the symlink. If you then run git clean, the symlink is removed, but the 'nested' repo will remain intact as it really resides elsewhere.

但是:如果用户在父存储库中运行 git clean -dfx,它将删除被忽略的嵌套存储库。另一种方法是符号链接文件夹并忽略符号链接。如果您然后运行 ​​git clean,符号链接将被删除,但“嵌套”存储库将保持完整,因为它确实位于其他地方。

回答by ephemient

git-subtreewill help you work with multiple projects in a single tree andkeep separable history for them.

git-subtree将帮助您在单个树中处理多个项目为它们保留可分离的历史记录。

回答by lachy

Summary.

概括。

Can I nest git repositories?

我可以嵌套 git 存储库吗?

Yes. However, by default git does not track the .gitfolder of the nested repository. Git has features designed to manage nested repositories (read on).

是的。但是,默认情况下 git 不会跟踪.git嵌套存储库的文件夹。Git 具有旨在管理嵌套存储库的功能(请继续阅读)。

Does it make sense to git init/add the /project_root to ease management of everything locally or do I have to manage my_project and the 3rd party one separately?

git init/add the /project_root 以简化本地所有内容的管理是否有意义,还是我必须分别管理 my_project 和第 3 方?

It probably doesn't make sense as git has features to manage nested repositories. Git's built in features to manage nested repositories are submoduleand subtree.

这可能没有意义,因为 git 具有管理嵌套存储库的功能。Git 用于管理嵌套存储库的内置功能是submodulesubtree

Here is a blog on the topicand here is a SO questionthat covers the pros and cons of using each.

这是有关该主题的博客,这是一个 SO 问题,涵盖了使用每种方法的优缺点。

回答by gnud

I would use one repository per project. That way, the history becomes easier to browse through.

我会为每个项目使用一个存储库。这样,历史就更容易浏览了。

I would also check the version of the third party library I'm using, into the repository of the project using it.

我还会将我正在使用的第三方库的版本检查到使用它的项目的存储库中。