在git中使用子模块来保持胆小 [英] using submodules in git for the timid

查看:89
本文介绍了在git中使用子模块来保持胆小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个项目中工作很少,每个项目都有独立的子项目。



看起来像这样:

  [Project C] [Project D] C和D使用B,因此A 
[项目B] B的所有源代码都在a中,并添加了更多
[Project A]

设置上级到父库并发出 git pull upstream 可以很好地从上级项目中获取更改。但是,如何在C影响与父层共享的代码的错误修复方案中推送提交?

如果我从Project A发出 git pull project-D ,它还会合并所有添加的源代码我还考虑使用子树子模块

解决方案


*我也考虑过使用子树子模块,但它看起来很复杂。*


这正是您需要使用的。并让我解释它是如何非常简单而又不复杂的


子模块 中,始终指向特定的提交。 p>






git submodule

将您的大项目分解为子项目,至此为止。

现在将每个子项项目给你主要项目使用:

  git submodule add< url> 

一旦投影被添加到您的回购库中,您必须初始化并更新它。

  git子模块init 
git子模块更新

Git 1.8.2 添加了新选项 - 远程

  git submodule update --remote --merge 



$ c>将它们合并到
签出 子模块的最新版本。



由于






git subtree

Git子树允许你插入任何一个仓库作为另一个仓库的子目录



非常类似于子模块,但主要区别在于代码的管理位置。在子模块中,内容被放置在一个单独的回购站中,并在那里进行管理,这样您就可以将其克隆到其他许多回购站。

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

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



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


I am working in a project which have few layers, each of them independent sub-projects.

It looks like this:

[ Project C ] [ Project D ]  C and D uses B and therefore A
[ Project B ]                B has all source code in a and adds more
[ Project A ]                

Setting upstream to the parent repository and issuing git pull upstream works well to get the changes from the upper project.

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

If I issue git pull project-D from Project A, it will also merge all the source code added at that layer.

I also considered using sub-trees or submodules, but it still looks complicated.

解决方案

* I also considered using sub-trees or submodules, but it still looks complicated.*

This is exactly what you need to use. and let me explain how its pretty simple and not so complicated

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 projected is added tot 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中使用子模块来保持胆小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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