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

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


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

git checkout seotweaksgit merge -s ours mastergit checkout mastergit 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


What about using git branch -m to rename the master branch to another one, then rename seotweaks branch to master? Something like this:

git branch -m master old-mastergit branch -m seotweaks mastergit push -f origin master

This might remove commits in origin master, please check your origin master before running git push -f origin master.


You can rename/remove master on remote, but this will be an issue if lots of people have based their work on the remote master branch and have pulled that branch in their local repo.
That might not be the case here since everyone seems to be working on branch 'seotweaks'.

In that case you can:
git remote --show may not work.(Make a git remote show to check how your remote is declared within your local repo. I will assume 'origin')
(Regarding GitHub, house9 comments: "I had to do one additional step, click the 'Admin' button on GitHub and set the 'Default Branch' to something other than 'master', then put it back afterwards")

git branch -m master master-old  # rename master on localgit push origin :master          # delete master on remotegit push origin master-old       # create master-old on remotegit checkout -b master seotweaks # create a new local master on top of seotweaksgit push origin master           # create master on remote

But again:

  • if other users try to pull while master is deleted on remote, their pulls will fail ("no such ref on remote")
  • when master is recreated on remote, a pull will attempt to merge that new master on their local (now old) master: lots of conflicts. They actually need to reset --hard their local master to the remote/master branch they will fetch, and forget about their current master.