Swap master branch with another branch in Git

In git, branch is just a pointer to the one of the commits. So you can create and remove branches very easily. Most of the time, you can select commit of your choice and create a new branch from their and delete the one you do not need. However, sometime you create one branch out of master, intending to merge it to master but later everyone starts using the same and it becomes kind of master. We ran into this situation with one of the source code repository. So to get back on track, we needed to swap the contents of the both branches. 

In this case, we can one of the specific merging strategies known as ours. As per documents, “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.”

So, let’s say the side branch name was feature-v1.3. So our commands to swap the code for feature-v1.3 and master branch would be:

git checkout feature-v1.3
git merge -s ours master
git checkout master
git merge feature-v1.3

Another use of this strategy is to skip a commit made on a maintenance branch that isn’t intended to go back into your main branch of development. So we can do selective merge from another branches if needed.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s