[英]Git - How can I use a specific remote branch as the upstream source for a repository?
Perhaps I'm thinking of a bad workflow, but here we go. 也许我想到的是糟糕的工作流程,但是现在我们开始吧。 I have a project that I would like to use as the base for other projects, since they all keep the same structure and share common classes.
我有一个我想用作其他项目基础的项目,因为它们都保持相同的结构并共享公共类。 Such base project is stored in its own Git repository (let's call it MyBaseRepo ), where two branches exist:
这样的基础项目存储在其自己的Git存储库中(我们将其称为MyBaseRepo ),该存储库中存在两个分支:
What I would like to do is the following: 我想做的是以下几点:
In short, I would like to put in place some sort of "repository inheritance", so that when base classes change, the child project can get the updates. 简而言之,我想进行某种“存储库继承”,以便在基类发生更改时,子项目可以获取更新。
So far I can almost do the above by fetching MyBaseRepo when needed and then merging the appropriate MyBaseRepo/branch with MyNewProject master (or whatever MyNewProject branch). 到目前为止,我几乎可以通过在需要时获取MyBaseRepo ,然后将适当的MyBaseRepo /分支与MyNewProject master(或任何MyNewProject分支)合并来完成上述操作。 Is this the proper way of doing it, or am I making my life more complicated than it should be?
这是正确的做法吗?还是让我的生活变得比应有的复杂? Thanks in advance for the answers.
预先感谢您的回答。
I think the way you described the problem (except the two branches part), you can go with git submodules . 我认为您描述问题的方式(除了两个分支部分),可以使用git submodules 。 With it, every revision of your project will be bound to a specific revision of the submodule.
有了它,项目的每个修订版都将绑定到子模块的特定修订版。
If you really need those two branches of MyBaseRepo
, I would do this way: 如果您确实需要
MyBaseRepo
两个分支, MyBaseRepo
可以这样做:
$ git init
Do a commit to initialize master
进行提交以初始化
master
Add the remote MyBaseRepo to the new repo: 将远程MyBaseRepo添加到新的仓库中:
$ git remote add MyBaseRepo the_repo_location
$ git fetch MyBaseRepo
$ git remote add MyBaseRepo the_repo_location
$ git fetch MyBaseRepo
$ git merge MyBaseRepo/master
or 要么
$ git merge MyBaseRepo/mybranch
. $ git merge MyBaseRepo/mybranch
。
But I would go with submodules. 但是我会使用子模块。 It's a "cleaner" solution - instead of merging from a remote branch, you'll only need to update the submodules.
这是一个“更干净”的解决方案-无需从远程分支合并,只需更新子模块。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.