Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace master branch in Git, entirely, from another branch? [duplicate]

I have two branches in my Git repository:

  1. master
  2. seotweaks (created originally from master)

I created seotweaks with the intention of quickly merging it back into master. However, that was three months ago and the code in this branch is 13 versions ahead of master.

It has effectively become our working master branch as all the code in master is more or less obsolete now.

Very bad practice I know, lesson learned.

Do you know how I can replace all of the contents of the master branch with those in seotweaks?

I could just delete everything in master and merge, but this does not feel like best practice.

like image 464
Jason Avatar asked May 19 '10 03:05

Jason


People also ask

How do I overwrite git master?

Using the -f flag, your previous master is completely overwritten with develop , including its history.


1 Answers

You should be able to use the "ours" merge strategy to overwrite master with seotweaks like this:

git checkout seotweaks git merge -s ours master git checkout master git merge seotweaks 

The result should be your master is now essentially seotweaks.

(-s ours is short for --strategy=ours)

From the docs about the 'ours' strategy:

This resolves any number of heads, but the resulting tree of the merge is always that of the current branch head, effectively ignoring all changes from all other branches. It is meant to be used to supersede old development history of side branches. Note that this is different from the -Xours option to the recursive merge strategy.

Update from comments: If you get fatal: refusing to merge unrelated histories, then change the second line to this: git merge --allow-unrelated-histories -s ours master

like image 146
ergosys Avatar answered Sep 21 '22 20:09

ergosys