Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rename master branch for both local and remote Git repositories

I have the branch master which tracks the remote branch origin/master.

I want to rename them to master-old both locally and on the remote. Is this possible?

For other users who tracked origin/master (and who always updated their local master branch via git pull), what would happen after I renamed the remote branch?
Would their git pull still work or would it throw an error that it couldn't find origin/master anymore?

Then, further on, I want to create a new master branch (both locally and remote). Again, after I did this, what would happen now if the other users do git pull?

I guess all this would result in a lot of trouble. Is there a clean way to get what I want? Or should I just leave master as it is and create a new branch master-new and just work there further on?

like image 927
Albert Avatar asked Oct 06 '09 16:10

Albert


People also ask

Can I rename master branch in git?

Renaming the Remote master Branch as Well In the second step, we'll have to create a new branch on the remote named "main" - because Git does not allow to simply "rename" a remote branch. Instead, we'll have to create a new "main" branch and then delete the old "master" branch.

How do I rename a branch in remote repository?

There isn't a way to directly rename a Git branch in a remote repository. You will need to delete the old branch name, then push a branch with the correct name to the remote repository.


2 Answers

The closest thing to renaming is deleting and then recreating on the remote. For example:

git branch -m master master-old git push remote :master         # Delete master git push remote master-old      # Create master-old on remote  git checkout -b master some-ref # Create a new local master git push remote master          # Create master on remote 

However, this has a lot of caveats. First, no existing checkouts will know about the rename - Git does not attempt to track branch renames. If the new master doesn't exist yet, git pull will error out. If the new master has been created. the pull will attempt to merge master and master-old. So it's generally a bad idea unless you have the cooperation of everyone who has checked out the repository previously.

Note: Newer versions of Git will not allow you to delete the master branch remotely by default. You can override this by setting the receive.denyDeleteCurrent configuration value to warn or ignore on the remote repository. Otherwise, if you're ready to create a new master right away, skip the git push remote :master step, and pass --force to the git push remote master step. Note that if you're not able to change the remote's configuration, you won't be able to completely delete the master branch!

This caveat only applies to the current branch (usually the master branch); any other branch can be deleted and recreated as above.

like image 115
bdonlan Avatar answered Sep 21 '22 13:09

bdonlan


Assuming you are currently on master:

git push origin master:master-old        # 1 git branch master-old origin/master-old  # 2 git reset --hard $new_master_commit      # 3 git push -f origin                       # 4 
  1. First make a master-old branch in the origin repository, based off of the master commit in the local repository.
  2. Create a new local branch for this new origin/master-old branch (which will automatically be set up properly as a tracking branch).
  3. Now point your local master to whichever commit you want it to point to.
  4. Finally, force-change master in the origin repository to reflect your new local master.

(If you do it in any other way, you need at least one more step to ensure that master-old is properly set up to track origin/master-old. None of the other solutions posted at the time of this writing include that.)

like image 45
Aristotle Pagaltzis Avatar answered Sep 22 '22 13:09

Aristotle Pagaltzis