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.
Since you have 2 branches, one is refactored significantly from the other, and you are maintaining both... i would say Git will not magically help you. For git to help, the patches/changes you apply to branch 1 need to be similar to branch 2 (but not exactly the same).
Since it was refactored, the code may not be similar, unless the new code is modularized to be the same on both branches.
What should you have done in the first place?
- add some unit test and/or integration test code to the initial branch
- run the same integration tests on the 2nd branch
- for new changes add tests
- you then can verify the changes work for both branches
Update:
For management of changes to both branches, you probably just need a process, such as:
- work primarily on branch-refactored
- merge every "story" or work item to branch-legacy as they are done
The second step is what i think would be a lot of work if the code is too different between the branches. If you do have to maintain both branches, try to encapsulate the new code in a library which can be used in both branches. That will make merges from one branch to the other cleaner.