在另一个 git 仓库中维护一个 Git 仓库

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

Maintaining a Git repo inside another git repo

gitgithub

提问by Gurbakhshish Singh

I have a git repo which contains an AngularJS web app.

我有一个 git repo,其中包含一个 AngularJS Web 应用程序。

It has a subfolder called build, which gets created by a gulp task. I am deploying to Azure, so it is connected directly to my bitbucket directory.

它有一个名为 的子文件夹build,它由 gulp 任务创建。我正在部署到 Azure,因此它直接连接到我的 bitbucket 目录。

I want to make build folder as a separate git repo from which the Azure app is being deployed. How do I achieve this in git??

我想将构建文件夹作为一个单独的 git 存储库,从中部署 Azure 应用程序。我如何在 git 中实现这一点?

回答by CodeWizard

You have several options like:

您有多种选择,例如:

  • submodule
  • subtree
  • 子模块
  • 子树


Submodulesallow foreign repositories to be embedded within a dedicated subdirectoryof the source tree, always pointed at a particular commit.

Submodules允许将外部存储库嵌入源树的专用子目录中,始终指向特定的提交。



git submodule

git submodule

Break your big project into 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.2new option --remotewas added

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

git submodule update --remote --merge

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

fetch来自上游的每个子模块的最新更改merge them in,以及子模块check out的最新版本。

As the docsdescribe 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.

--remote

此选项仅对更新命令有效。不要使用超级项目记录的 SHA-1 来更新子模块,而是使用子模块的远程跟踪分支的状态。

This is equivalent to running git pull in each submodule.

这相当于在每个子模块中运行 git pull 。



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

但是,在影响与父层共享的代码的 C 中的错误修复方案中,我将如何推送提交?

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.

这是子模块 - 另一个项目中的项目的说明,其中每个项目都是一个独立的项目。

enter image description here

在此处输入图片说明



git subtree

git subtree

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

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

Very similar to submodulebut 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.

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

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

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

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/

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

回答by FZE

You can do this with git submodule or subtree, I use submodule for this kind of reason.

您可以使用 git submodule 或 subtree 来完成此操作,出于这种原因,我使用 submodule。

https://git-scm.com/docs/git-submodule

https://git-scm.com/docs/git-submodule

example :

例子 :

/mainrepository
/mainrepository/subrepository

cd /mainrepository/subrepository;
git init .
cd ../
git submodule add ./subrepository

then open seperate remote repository in bit bucket then 
cd into ./subrepository
git remote add origin https://bitbucket.com/path/to/subrepository.git

basically it is all about that.

基本上就是这样。

I have no detailed information about subtrees what I know, it is more advanced than submodules. But if your needs are basically matches with submodules it is easy to maintain.

我没有我所知道的关于子树的详细信息,它比子模块更高级。但是如果您的需求与子模块基本匹配,则易于维护。