Git – How to Change Master Branch

29 / Aug / 2011 by Hitesh Bhatia 0 comments

While working on Git with a big team, there are chances that a situation might arise when you want to set some other branch as master branch.Recently we were in a same situation. Several of branches were merged to master branch. And unfortunately one of the branches merged was created from an unstable branch. Making our master branch unstable. And we cherry-picked all stable changes to masterTemp branch.So we landed in a situation where we wanted masterTemp branch as master branch.And Here is how we did it.

1 ) Renamed master branch to oldmaster.

git branch -m master oldmaster


Now there is no master branch on my local machine.

2) Renamed my masterTemp branch to master

git branch -m masterTemp master

The branch which was named masterTemp on my local machine is now master

3) Delete the branch from remote

git branch -rD master

4) Push the new master branch to remote

git push --force origin master

And its done.We had to perform step 3 and 4 because git does not allows us to delete master branch from remote. i.e git push origin :master wont work. Hence we forcefully pushed out new master branch to remote. Plus anyone who is working on same repo should delete their master branch , take git pull and create new master branch.
(i.e git branch -d master; git branch -rD master;git pull ; git checkout -b master origin/master )

_________________________________
Hitesh Bhatia

_________________________________

FOUND THIS USEFUL? SHARE IT

Leave a comment -