如何将文件夹从 git repo 链接到另一个 repo? [英] How to link folder from a git repo to another repo?

查看:75
本文介绍了如何将文件夹从 git repo 链接到另一个 repo?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个公共存储库来放置我的主存储库(私有)中的一些示例文件.有没有办法将一个 git 仓库中的几个文件夹软链接到另一个 git 仓库?

解决方案

那么你应该使用子模块来完成这个任务.

子模块是同一个根下不同的git仓库.
这样您就可以在根存储库内的文件夹级别管理 2 个不同的项目

<块引用>

Submodules 允许将外部存储库嵌入到专用子目录中源代码树,总是指向一个特定的提交.


git submodule

像到目前为止那样将您的大项目分解为子项目.
现在使用以下命令将每个子项目添加到您的主项目中:

git submodule add 

将项目添加到您的存储库后,您必须对其进行初始化和更新.

git 子模块初始化git子模块更新

Git 1.8.2 开始,添加了新选项 --remote

git submodule update --remote --merge

获取上游每个子模块的最新变化,将它们合并到,以及检查子模块的最新版本.

正如


git subtree

Git 子树允许您将任何存储库作为另一个存储库的子目录插入

submodule 非常相似,但主要区别在于代码的管理位置.在子模块中,内容被放置在一个单独的存储库中并在那里进行管理,这允许您将其克隆到许多其他存储库.

subtree 将内容作为根项目的一部分进行管理,而不是在单独的项目中.

无需写下如何设置和了解如何使用它,您只需阅读这篇出色的文章,它将解释这一切.

https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/

I want to create a public repo to put some sample files from my main repo (private). Is there any way to soft link few folders from a git repo to another git repo?

解决方案

Then you should use submodules for this task.

Submodule are different git repositories under the same root.
This way you can manage 2 different project at folder level inside the root repository

Submodules allow foreign repositories to be embedded within a dedicated subdirectory of the source tree, always pointed at a particular commit.


git submodule

Break your big project to sub projects as you did so far.
Now add each sub project to you main project using :

git submodule add <url>

Once the project is added to your repo, you have to init and update it.

git submodule init
git submodule update

As of Git 1.8.2 new option --remote was added

git submodule update --remote --merge

will fetch the latest changes from upstream in each submodule, merge them in, and check out the latest revision of the submodule.

As the docs describe it:

--remote

This option is only valid for the update command. Instead of using the superproject’s recorded SHA-1 to update the submodule, use the status of the submodule’s remote-tracking branch.

This is equivalent to running git pull in each submodule.


However, how would I push a commit in the scenario of bug fix in C which affects the code shared with the parent layers?

Again: using submodule will place your code inside your main project as part of its content. The difference between having it locally inside the folder or having it as part of a submodule is that in submodule the content is managed (commited) to a different standalone repository.


This is an illustration of submodule - project inside another project in which each project is a standalone project.


git subtree

Git subtree allows you to insert any repository as a sub-directory of another one

Very similar to submodule but the main difference is where your code is managed. In submodules the content is placed inside a separate repo and is managed there which allow you to clone it to many other repos as well.

subtree is managing the content as part of the root project and not in a separate project.

Instead of writing down how to set it up and to understand how to use it you can simply read this excellent post which will explain it all.

https://developer.atlassian.com/blog/2015/05/the-power-of-git-subtree/

这篇关于如何将文件夹从 git repo 链接到另一个 repo?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆