简体   繁体   English

Git-如何使用特定的远程分支作为存储库的上游源?

[英]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 ),该存储库中存在两个分支:

  • master , containing a very stripped down version of the project. master ,其中包含项目的精简版本。
  • mybranch , containing everything included in master, plus more specific classes. mybranch ,包含master中包含的所有内容,以及更具体的类。

What I would like to do is the following: 我想做的是以下几点:

  • Create a new project, eg MyNewProject , cloning one of the two branches from MyBaseRepo . 创建一个新项目,例如MyNewProject ,从MyBaseRepo克隆两个分支之一
  • Create a new MyNewProjectRepo Git repository as the origin for the new project. 创建一个新的MyNewProjectRepo Git存储库,作为新项目的源。
  • Push/pull all changes related to the implementation of MyNewProject to MyNewProjectRepo . 将与MyNewProject的实现相关的所有更改推/拉到MyNewProjectRepo
  • When MyBaseRepo gets updated, fetch the changes from the appropriate branch. 更新MyBaseRepo后 ,请从相应的分支中获取更改。

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可以这样做:

  • Initialize an empty repo: 初始化一个空的仓库:

$ 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

  • Then, when needed, merge: 然后,在需要时合并:

$ 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.

相关问题 如何删除本地git存储库中的远程分支? - How can I delete a remote branch in a local git repository? 我可以通过 ruby-git 获取上游远程分支吗 - Can I get the upstream remote branch by ruby-git 如何查看哪个 Git 分支正在跟踪哪个远程/上游分支? - How can I see which Git branches are tracking which remote / upstream branch? 我怎么知道哪些补丁不包含与上游git存储库在同一分支中提交的git存储库? - How may I know what patches are exclusive of a git repository that commits in the same branch as the upstream git repository? Git:如何将远程分支的上游设置为另一个远程分支? - Git: How set upstream of a remote branch to another remote branch? 如何检查特定远程存储库分支是否包含特定的合并分支? - How can I check if a specific remote repository branch contains a specific merged branch? 如何在不克隆整个存储库的情况下显示远程git存储库的master分支的修订日期? - How can I show the revision date of a remote git repository's master branch, without cloning the whole repository? 如何将 git master 分支重置为分叉存储库中的上游分支? - How do I reset the git master branch to the upstream branch in a forked repository? 如何在远程存储库中记录分支? - How can I document a branch in a remote repository? 我可以使用本地存储库作为git远程服务器吗 - Can I use a local repository as a git remote
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM