Git:如何在现有的git子回购中使用git子树? [英] Git: How to use git subtree with existing git sub repo?

查看:154
本文介绍了Git:如何在现有的git子回购中使用git子树?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 parent_repo 和一个 sub_repo ,就像这样:

  .\parent 
.\parent\parent_file.html
.\parent\.gitignore
。 \parent\lib\sub_repo
.\parent\lib\sub_repo\sub_file.html
.\parent\lib\sub_repo\.gitignore

在我知道子树子模块,我为这两个回购使用git。但是当人们看到 parent_repo 时,他们看不到 sub_repo 的代码。





建议我应该使用子树子模块






git subtree



Git子树允许您将任何存储库作为另一个子目录插入
$ b 非常类似于 submodule ,但主要区别在于代码的管理位置。在子模块中,内容被放置在一个单独的回购站中,并在那里进行管理,这样您就可以将其克隆到其他许多回购站。

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

<而不是写下如何设置和理解如何使用它,你可以简单阅读这个优秀的文章,这将解释这一切。



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


I have a parent_repo and a sub_repo within it, like this:

.\parent
.\parent\parent_file.html
.\parent\.gitignore
.\parent\lib\sub_repo
.\parent\lib\sub_repo\sub_file.html
.\parent\lib\sub_repo\.gitignore

Before I know about subtree or submodule, I use git for both of the repos. But when people see the parent_repo, they cannot see the sub_repo 's code.

It is suggested that, I should use subtree or submodule. What does a grey icon in remote GitHub mean

Then What should I do? To make the sub_repo code available in parent_repo?


I looked at the docs of submodule and subtree. It seems like subtree is better than submodule. But the tutorial are not very illustrative (https://medium.com/@v/git-subtrees-a-tutorial-6ff568381844#.aztjizd8g).

I run git subtree add —-prefix=lib/sub_repo my-subtree master, and it reports prefix lib/sub_repo already exists.

Is there any simpler example that I can follow?

解决方案

First let's explain what is the main difference between subtree and submodule:

both of them are used for having another repo inside existing repo. The main difference is that git submodule is independent self-contained repository while subtree store the date in the parent (original) repo.


Now let's dig in and explain in more details:

Is there any simpler example that I can follow?

Submodule is a standalone git project so the code will be checked out to a new folder under the root folder and it's not part of your master branch.

Your root folder will contain a submodule file and you will have to init && update it on every clone you make.

# Add the desired submodule to your code base
git submodule add <url>

You must run two commands:

git submodule init 

to initialize your local configuration file, and

git submodule update 

to fetch all the data from that project and check out the appropriate commit listed in your superproject:

So the full script is this:

git submodule add <url>
git submodule init
git submodule update

You simply need to be in your root folder and then add the submodule folder.

git submodule add <url>

Now when you clone the project you simply need to init and update the submodule

git submodule init
git submodule update

Git 1.8.2 features a new option --remote

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.


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:如何在现有的git子回购中使用git子树?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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