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

Asked 2023-09-20 20:57:35 View 473,004

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.

  • Also, this question is better worded and is missing the confusion caused by the "Extra" comment added to the question after answers had started. - anyone
  • There is a simple way using checkout, if and only if you are not worried about commit histories of seotweaks. Switch to master branch first, then run, git checkout seotweaks -- * then run, git commit -m "Replaced contents of master with seotweaks" finally run git push - anyone

Answers

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

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

The first two steps are a useful precaution to ensure your local copy of master is up-to-date. The result should be that 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

Answered   2023-09-20 20:57:35

  • @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. - anyone
  • 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? - anyone
  • @elsurudo It's probably not an error: stackoverflow.com/a/634575/240633 - anyone
  • the rename strategy posted by @ZelluX below better solves this problem. This answer will just merge a cleaned up branch on top of your existing master - anyone
  • If you get fatal: refusing to merge unrelated histories, then change the second line to this: git merge --allow-unrelated-histories -s ours master - anyone

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.

Answered   2023-09-20 20:57:35

  • @Jason: Try git push -f origin master - anyone
  • This is probably the best way to do a forced update after ensuring your master can be completely replaced - anyone
  • 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. - anyone
  • Crap I just lost all commits in the original master. - anyone
  • @moberme you can do git checkout old-master && git push origin old-master to create a branch with the old master. - anyone

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.

Update/Note 2022:

    git branch -m main main-old  # rename main on local
    git push origin :main          # delete main on remote
    git push origin main-old       # create main-old on remote
    git switch -c main seotweaks # create a new local main on top of seotweaks
    git push origin main           # create main on remote

Answered   2023-09-20 20:57:35

  • 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. - anyone
  • @Jason: I changed that to 'origin' which might be the default name given to your remote repo. - anyone
  • @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. - anyone
  • @kriss: GitHub will refuse by default any push rewriting/removing history, unless you force the push: git push -f origin :master. - anyone
  • 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 - anyone

Since seotweaks was originally created as a branch from master, merging it back in is a good idea. However if you are in a situation where one of your branches is not really a branch from master or your history is so different that you just want to obliterate the master branch in favor of the new branch that you've been doing the work on you can do this:

git push [-f] origin seotweaks:master

This is especially helpful if you are getting this error:

! [remote rejected] master (deletion of the current branch prohibited)

And you are not using GitHub and don't have access to the "Administration" tab to change the default branch for your remote repository. Furthermore, this won't cause down time or race conditions as you may encounter by deleting master:

git push origin :master

Answered   2023-09-20 20:57:35

  • Does not work on Heroku: ! [rejected] <new_branch> -> master (non-fast-forward) error: failed to push some refs to '<some_git>.git' - anyone
  • git push -f origin seotweaks:master worked for me - anyone
  • This was by far the easiest way for me to get extensive changes on a working branch, with many difficult conflicts (due to folders being deleted and renamed), back onto master. I don't know if this is appropriate for every case (probably not), but it totally worked for me just getting everything off a branch I was done with back onto master (the working branch remains after the operation, but both branches appear to now have the same commits). - anyone
  • How does the master git history looks like after force push? Does it overwrite all files on master even when they weren't changed on seotweaks? - anyone

I found this to be the best way of doing this (I had an issue with my server not letting me delete).

On the server that hosts the origin repository, type the following from a directory inside the repository:

git config receive.denyDeleteCurrent ignore

On your workstation:

git branch -m master vabandoned                 # Rename master on local
git branch -m newBranch master                  # Locally rename branch newBranch to master
git push origin :master                         # Delete the remote's master
git push origin master:refs/heads/master        # Push the new master to the remote
git push origin abandoned:refs/heads/abandoned  # Push the old master to the remote

Back on the server that hosts the origin repository:

git config receive.denyDeleteCurrent true

Credit to the author of blog post http://www.mslinn.com/blog/?p=772

Answered   2023-09-20 20:57:35

  • For me master branch was the default branch so I changed default branch as 'develop' branch and deleted master branch and created again master from desired branch. Later if you want you can make 'master' branch again your default branch. - anyone
  • The link is broken, "Access Denied". - anyone
  • Shame. I think the blogs do exist on his site still. Just a broken link :( - anyone
  • It changed. Now it shows an index of posts (and they seem to be accessible). - anyone