Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How to properly merge two functional and quite different branches?

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?

like image 959
jayarjo Avatar asked Jan 09 '13 18:01

jayarjo


1 Answers

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.

like image 105
jayarjo Avatar answered Oct 18 '22 20:10

jayarjo