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.

[shell]git branch -m master oldmaster[/shell]

Now there is no master branch on my local machine.

2) Renamed my masterTemp branch to master

[shell]git branch -m masterTemp master[/shell]

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

3) Delete the branch from remote

[shell]git branch -rD master[/shell]

4) Push the new master branch to remote

[shell]git push –force origin master[/shell]

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



Leave a Reply

Your email address will not be published. Required fields are marked *