Git: Replace the master branch with a feature branch

Amit Mutreja
2 min readDec 27, 2016

--

A product team has to work on different features at a time. The team members may create different branches to push their code so that dependency on others’ code is minimum while developing a feature and merge it later before releasing the feature. It is possible that master branch may become irrelevant in this case and you might want to replace master branch altogether with a feature branch.

Although there is no way to directly des­ig­nate your cur­rent branch to replace the mas­ter, you can how­ever do this indi­rectly by using a merge strategy.

If you check­out your feature branch and merge the mas­ter into it with the ‘ours’ strat­egy, it has the effect of absorb­ing the mas­ter into your cur­rent branch but not using any­thing of the mas­ter. This way, when you check­out the mas­ter and do an ordi­nary fast for­ward merge of your fea­ture branch, the merge com­mit will be exactly like your fea­ture branch, effec­tively mak­ing it seems like you replaced the mas­ter with your fea­ture branch.

Step by step guide:

Checkout to the feature branch

git checkout feature_branch

Merge with master. If you want your history to be a little clearer, I’d recommend adding some information to the merge commit message to make it clear what you’ve done.

git merge -s ours --no-commit master
git commit # add information to the template merge message

Checkout to master and do a normal merge with feature branch

git checkout master
git merge feature_branch #fast-for­ward merge

These changes are done only in your local git repository. To push these changes to GitHub do:

git push

--

--

Amit Mutreja

BITS-Pilani Technocrat, Learner, Thinker, Dreamer, Traveler, Programmer. Other blog: http://viparitadisha.blogspot.com