how to move master branch to main branch on github

Viewed 1668

I have a master branch and a main branch in git-hub. This was not intentional I realized that I have been pushing with master so everything went to master branch. However git-hub is now using main instead of master as first position. How can I move my master branch into main branch?

4 Answers

There are multipe ways to achieve this, but with pure git it can be done as follows:

#Change to the main branch
git checkout main
#Rebase the main branch on top of the current master. In easy words, go to the last point the two branches have in common, then add all the changes master did in the meanwhile and then add the changes done to main. If main is empty this is equivalent to 
# git checkout master; git branch -D main; git checkout -b main
#which deletes the current main and then copies master over to main
git rebase master
# push back to GitHub (--force, because rabse does not add a commit, but changes the status, so push will be rejected without it)
git push --force

Create a PR from master to main in the GitHub Webinterface. If you want to do it local:

git checkout main

git merge master

git push

Push it into the main branch in GitHub.

You can delete or rename a branch through the github web interface by visiting

https://github.com/YourName/YourRepo/branches

At time of writing you can access that page from the front page of your repository through the branches link: https://i.imgur.com/64Zc2n6.png

And you can click the writing implement buttons to rename their associated branch: https://i.imgur.com/tn7BqPS.png

It is worth noting that deleting and renaming branches through github is just as potentially destructive as doing it with CLI git or a GUI git client. You can of course re-push branches from your local clone, but you should make sure that clone is up to date before proceeding if you feel unsure.

Related