Back

Explore Courses Blog Tutorials Interview Questions
+4 votes
2 views
in DevOps and Agile by (29.3k points)
edited by

I have two branches in my Git repository:

1. master

2. child (created originally from master)

I created a child intending to quickly merge 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 the master is more or less obsolete now.

The 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 a child?

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

2 Answers

+1 vote
by (50.2k points)
edited by

To replace a master branch in git entirely from a branch we are going to use a merge strategy known as ‘ours’.

Let’s see how this strategy works 

For that, we have a master and child branch

First, checkout child branch

git checkout child

Now merge using ‘ours’

git merge -s ours master

-s is a shortcut for -strategy

Goto master

git checkout master

git merge child

Then the result should be your master is now a child.

For more information please go through the following tutorial to get more info about git:

+3 votes
by (19.4k points)

Since child was originally created as a branch from master, merging it back in is a good idea. However, in case 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 child:master

This command is going to be helpful if you are getting the following error:

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

Also, it is helpful when you are not using GitHub and do not have access to the "Administration" tab to change the default branch for your remote repository. 

On the bright side, this won't cause downtime or race conditions as you may encounter by deleting master:

git push origin :master

Browse Categories

...