Categories
git git-branch merge

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

1994

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.

6

  • 45

    re: exact duplicate flag– that may be true, but ergosys’s answer below is better than the accepted answer to that question, IMO

    Dec 19, 2012 at 0:25

  • 33

    note it may be declared a duplicate but is the first hit on a web search for “git delete branch master”

    – peterk

    Jul 9, 2015 at 15:44

  • 35

    These points rarely matter to the dedicated duplicate-zealot.

    – geotheory

    Apr 26, 2016 at 11:50

  • 3

    Also, this question is better worded and is missing the confusion caused by the “Extra” comment added to the question after answers had started.

    May 10, 2016 at 11:20

  • You can also check this solution stackoverflow.com/a/3790682/1770571

    Mar 4, 2019 at 10:36

3358

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

42

  • 46

    May be too late to add this question but what is wrong with this: git checkout master;git merge -s theirs seotweaks – saves a couple of steps.

    Sep 22, 2011 at 15:12


  • 34

    @Joel Berger, the recursive merge options will mix the two branches, favoring “theirs” or “ours” only on conflict. So you’ll get changes from both branches.

    – ergosys

    Oct 21, 2011 at 18:23

  • 46

    This isn’t working for me. When I do “git merge -s ours master” from the other branch, I get “Already up-to-date.” Anything else I can try?

    – elsurudo

    Mar 9, 2012 at 16:58

  • 14

    Didn’t work for me

    – pratnala

    Mar 20, 2014 at 18:03

  • 16

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

    – code4kix

    Aug 21, 2018 at 13:45

592

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-master
git branch -m seotweaks master
git push -f origin master

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

15

  • 36

    @Jason: Try git push -f origin master

    – Koraktor

    May 19, 2010 at 8:08

  • 11

    This is probably the best way to do a forced update after ensuring your master can be completely replaced

    – Hrishi

    Feb 16, 2014 at 16:20

  • 16

    It might be worth explaining that this answer possibly removes commits that were in the original master branch. While ergosys’ solution does a proper merge and so retains all history in master.

    – florisla

    Aug 28, 2015 at 8:05

  • 54

    Crap I just lost all commits in the original master.

    – moberme

    Nov 19, 2015 at 17:50

  • 5

    @moberme you can do git checkout old-master && git push origin old-master to create a branch with the old master.

    May 11, 2016 at 10:06

86

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 local
git push origin :master          # delete master on remote
git push origin master-old       # create master-old on remote
git checkout -b master seotweaks # create a new local master on top of seotweaks
git 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.

6

  • Thanks for the detailed response, when I run ‘git push remote :master’ I get an error – ‘remote’ does not appear to be a git repository.

    – Jason

    May 19, 2010 at 4:02

  • @Jason: I changed that to ‘origin’ which might be the default name given to your remote repo.

    – VonC

    May 19, 2010 at 4:08

  • 1

    @VonC: I’m trying to do that on git-hub repository, but when trying to perform ‘git push origin :master’ I get a message ‘[remote rejected] master (deletion of the current branch prohibited)’. As for why I’m doing that… basically I severely mixed up things, importing two times the same patches through github interface and command line push, then getting everything back to work by manual merge. After that I also created another branch with a clean history, but too late… anyway. As it’s on my personal experimental repository I should be the only one impacted.

    – kriss

    Aug 24, 2011 at 8:00


  • 5

    @kriss: GitHub will refuse by default any push rewriting/removing history, unless you force the push: git push -f origin :master.

    – VonC

    Aug 24, 2011 at 8:08

  • 4

    thanks this was a big help; 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

    – house9

    Oct 12, 2011 at 19:07