od107
od107

Reputation: 67

git abandon master branch

In my git repository I've created a new branch called alt. After some work it seems that the solution in alt outperforms the one in my master branch, so I want to abandon the master and make alt the new master.

             C1
              |
             C2
             /\
master ->  C3  C4
               |
               C5  <- alt 

So I want to abandon C3, but still keep it in the commit history for future reference, and go back to one branch. What is the best way to do this?
My first idea is to move master to C5, and then merge alt into master. Is this correct? Do I even need to move master first?
I could also just merge master into alt, resolve all merge conflicts by keeping the version from alt, and then rename alt to master.
Or should I move master to alt and simply remove branch alt, leaving C3 as a discontinued branch, which is actually the reality?
I think there are multiple solutions here, is there a best practice to be followed (always merging branches back into master vs open-ended ones)?

Upvotes: 4

Views: 788

Answers (1)

musiKk
musiKk

Reputation: 15189

I think the easiest way would be to merge master into alt with the ours merge strategy. This creates a new commit C6 whose tree is exactly the same as C5 but it has C3 and C5 as a parent, thereby keeping C3 in the repository.

git checkout alt
git merge --strategy=ours master # perform the merge
git checkout master
git merge --ff-only alt          # move master to alt
git branch -d alt                # delete alt if it's not needed anymore

Instead of merging, you should also be able to rebase if you want a linear history. Just replace the first two lines with git rebase --strategy=ours alt master.

Please check every command you enter. I haven't tested this. Of course, git being git, nothing is ever lost if something goes wrong...

Upvotes: 4

Related Questions