简体   繁体   English

Git在两个存储库中同步两个名为“生产”的分支

[英]Git syncing two branches called "production" in two repos

I've seen similar questions about this, but none that give me confidence to risk mangling my git.我已经看到了类似的问题,但没有一个让我有信心冒着破坏我的 git 的风险。 I'm not a total noobie, but have never had the need to deal with two repos so I want to know the right way to do this.我不是一个完全的菜鸟,但从来没有需要处理两个回购所以我想知道正确的方法来做到这一点。

I'm working on a project where we have one repo for staging ( SRV-S ) and another repo for production ( SRV-P ).我正在开发一个项目,其中我们有一个用于登台 ( SRV-S ) 的存储库和另一个用于生产的存储库 ( SRV-P )。 Both have "production" branches that go live if pushed to, via Jenkins magic.两者都有“生产”分支,如果推送到,通过 Jenkins 魔法就会上线。

I'm not a git wiz so I maintain separate project folders for each repo.我不是 git wiz,所以我为每个 repo 维护单独的项目文件夹。 I make changes on SRV-S (staging repo, production branch) and once approved, manually copy those files over to my SRV-P folder (production repo, production branch).我对SRV-S (暂存仓库、生产分支)进行了更改,一旦获得批准,将这些文件手动复制到我的SRV-P文件夹(生产仓库、生产分支)。 Then I cd into SRVF-P and git add, commit, push.然后我 cd 进入SRVF-P并 git add、commit、push。 Tedious.乏味。

Is there an easy way I can have be in the staging repo, on the branch named 'production', then pull a branch named 'production' from another repo, then push to either repo.有没有一种简单的方法可以让我进入暂存仓库,在名为“生产”的分支上,然后从另一个仓库中拉出一个名为“生产”的分支,然后推送到任一仓库。 ie that 'production' branch would exist in two repos with the same history.即“生产”分支将存在于具有相同历史记录的两个存储库中。

I think your major problem is that the repos are not really tied, are they?我认为你的主要问题是回购并没有真正捆绑,是吗? Like, you have two separate repos, two branches with the same name... but they have always run in parallel, no common ancestor.就像,你有两个独立的仓库,两个同名的分支……但它们总是并行运行,没有共同的祖先。 If that's the case, you can still play with a single local repo where you will work and you have two remotes set up on it (srv-p, srv-s).如果是这种情况,您仍然可以使用一个本地存储库来工作,并且在其上设置了两个遥控器(srv-p、srv-s)。 Now.... I guess you will be mostly doing work against srv-s... and then at some point you decide that what you have in srv-s/production is what you would like to have on srv-p/production (content-wise, not history-wise).现在.... 我猜你将主要针对 srv-s 做工作...然后在某个时候你决定你在 srv-s/production 中拥有的就是你想要在 srv-p/production 中拥有的(内容方面,而不是历史方面)。

The way to do that with the least effort and without having to move stuff around by hand is:以最少的努力做到这一点并且无需手动移动东西的方法是:

git checkout srv-s/production
# you would be on detached HEAD

# next step is where the magic happens:
# - move branch "pointer" to srv-p/production
# - keep our content as is, all differences between our working tree
#   and srv-p/production will be on index, ready to be committed
git reset --soft srv-p/production

# now we create a new revision so that the two branches have
# exactly the same content
git commit -m "A new version in srv-p/production"

# now we push into srv-p/production cause right now we are still
# on detached HEAD
# we actually haven't moved any branch either locally
# or on any of the two remotes
git push srv-p HEAD:production # send current HEAD to srv-p/production

I am doing this without any local branches, you could get the flow going with local branches as well.我在没有任何本地分支机构的情况下执行此操作,您也可以通过本地分支机构获得流程。

The core issue is that you have a bad versioning strategy.核心问题是你有一个糟糕的版本控制策略。 You should not be maintaining two repos.应该维护两个存储库。

Merge the repos into one and maintain a separate branch for production and staging.将 repos 合并为一个,并为生产和暂存维护一个单独的分支。 Then merge between the two branches.然后在两个分支之间合并。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM