Git:如何正确合并两个功能完全不同的分支? [英] Git: How to properly merge two functional and quite different branches?

查看:746
本文介绍了Git:如何正确合并两个功能完全不同的分支?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下你有两个分支在同一个项目中的情况,第一个分支的一部分会显着重构另一个分支。但是有一段时间你需要保持两个分支的功能,所以你正在对它们进行错误修复和重要的功能添加,有时是以不对称的方式。而且在某个时刻,有一段时间你必须将重构分支合并到原始分支上。在这种情况下使用最好的技术是什么?是否仍有可能保持历史清洁?



但更重要的是,在这种情况下应该是我的初始策略?

master ,因此您可以简单地移除 master 完全分支或将其重命名为让我们说 - legacy ,然后取另一个分支并将其重命名为 master 。而已。以下是您可能需要执行的实际命令,以实现本地和GitHub上的目标:

  git branch -m master master#将本地master重命名为legacy 
git checkout legacy
git branch -m another_branch master#another_branch将成为我们的新主人

本地我们现在就完成了。但是你不能简单地在GitHub上删除 master 分支。您需要先将另一个分支作为默认分支。这可以在储存库设置>中进行。默认分支。一旦你这样做,你可以继续:

  git push origin:master#删除GitHub上的master 
git push origin master#推出我们的新主分支
git push origin legacy#推送我们的传统分支

然后回到设置>默认分支并将默认分支切换回 master 。此外,您可以删除在迁移过程中可能创建的所有额外分支。



或者,如果您希望将所有操作保存在历史记录中,请检查正确答案< a href =https://stackoverflow.com/questions/2763006/change-the-current-branch-to-master-in-git>这里。


Imagine a situation where you have two branches of the same project, with one part of the first branch dramatically refactored over the other one. But for a while you need to keep both branches functional, so you are doing bug fixes and crucial feature additions to both of them, sometimes in a not symmetrical way. And at some point there comes a moment when you have to merge refactored branch onto the original one. What is the best technique to use in a situation like this? Is it still possible to keep the history clean?

But more importantly what should have been my initial strategy in such scenario?

解决方案

Since the task was to simply use another branch instead of master, you can simply remove master branch completely or rename it to let's say - legacy, then take another branch and rename it to master. That's it. Here are actual commands that you might need to execute to achieve the goal locally and on GitHub:

git branch -m master legacy               # rename local master to legacy
git checkout legacy
git branch -m another_branch master       # another_branch will be our new master

Locally we are done now. However you cannot simply remove master branch on GitHub. You need to take another branch as default first. This can be done in repository Settings > Default Branch. Once you do this, you can proceed:

git push origin :master                   # remove master on GitHub
git push origin master                    # push out our new master branch
git push origin legacy                    # push our legacy branch too

Then go back to Settings > Default Branch and switch default branch back to master. Additionally you can remove all extra branches that you might have created during migration process.

Alternatively, if you are looking to save all your actions in history, check a correct answer here.

这篇关于Git:如何正确合并两个功能完全不同的分支?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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