How to Overwrite Master With Branch in Git

Abdul Jabbar Feb 02, 2024
How to Overwrite Master With Branch in Git

Git is used to maintaining track of the source code that we are using; it also facilitates collaboration and helps us preserve our project to the current state.

When we develop new features, their history should be at our fingertips, as it’s very helpful in developing any application or documentation.

Git Overwrite Master With Branch

Git has two ways to mingle the changes from one branch to another. One is rebased, and the other is merged with any branch into another repository branch.

This article will discuss merging the master branch in Git completely from another repository branch.

While using Git workflows, the changes that we have made to our code must finally be concluded in the master branch when the application tasks are completed.

We must also understand that we might have some other branch that we have developed, which holds code changes that are not yet ready for production deployment, and we have named this branch as dev according to the organizational requirements and rules.

In some cases, we have made too many changes in our dev branch, and then after that, we face difficulty while combining the dev branch with our master branch; it cannot be performed easily.

One way to overcome this hectic situation is to entirely supersede our master branch with the dev branch. We can accomplish it in two ways which are explained below.

Merge Strategy as Ours

To accomplish this strategy, we will first execute the following commands to merge the dev branch into the master branch with the help of the ours merge strategy, which is the following.

git checkout dev
git merge -s ours master
git checkout master
git merge dev

The option --strategy=ours in the merge is meant to replace the old history of feature branches. Now our master will have all the contents of our dev and ignore all changes in the master.

By applying this method, we will get a clean and safe merge commit; other developers using these branches can also have the advantage of this merging as they won’t experience problems in merging their feature branches.

On the other hand, the drawback of this method is that this merge might not work if our dev branch and master branches have radiated to a larger scale in a project.

Force Pushing

The other option is to force push the dev branch under a different name, but this method is savage from the above issue discussed.

git push -f origin dev: master

With the help of the alias -f flag mentioned in the command, our earlier branch master is entirely overwritten with the dev branch, including the history.

We must be very careful while applying the method above because it will remove all the commits present in the master branch as they are also not available in the dev branch of the repository.

Author: Abdul Jabbar
Abdul Jabbar avatar Abdul Jabbar avatar

Abdul is a software engineer with an architect background and a passion for full-stack web development with eight years of professional experience in analysis, design, development, implementation, performance tuning, and implementation of business applications.

LinkedIn

Related Article - Git Branch