简体   繁体   English

如何在 Github 存储库上将主分支重命名为 master

[英]How to rename main branch to master on a Github repository

I'd like to create a github repository and rename the main branch to master .我想创建一个 github 存储库并将main分支重命名为master

If I create a new repository on github and do如果我在 github 上创建一个新的存储库并执行

git init
git add README.md
git commit -m "first commit"
git branch -M master

I get我得到

error: refname refs/heads/master not found
fatal: Branch rename failed

so I seem to somehow not understand git well enough.所以我似乎不太了解 git。 What's the issue here?这里有什么问题?


I must have gotten confused when I was playing around with the above.当我在上面玩的时候,我一定很困惑。 The following happens:发生以下情况:

git init create the repository git init创建存储库

git add README.md adds the file git add README.md添加文件

git commit -m "first commit" adds the file to the master branch since that still default for git git commit -m "first commit"将文件添加到master分支,因为这仍然是 git 的默认值

so I never have to rename it in the first place.所以我一开始就不必重命名它。 Now Github uses the default main, which can be changed in settings -> repositories on github.com现在 Github 使用默认的 main,可以在 github.com 上的设置 -> 存储库中更改

I'd like to create a github repository and rename the main branch to master .我想创建一个 github 存储库并将main分支重命名为master

If I create a new repository on github and do如果我在 github 上创建一个新的存储库并执行

git init
git add README.md
git commit -m "first commit"
git branch -M master

I get我得到

error: refname refs/heads/master not found
fatal: Branch rename failed

so I seem to somehow not understand git well enough.所以我似乎不太了解git。 What's the issue here?这里有什么问题?


I must have gotten confused when I was playing around with the above.当我玩弄上面的东西时,我一定很困惑。 The following happens:发生以下情况:

git init create the repository git init创建存储库

git add README.md adds the file git add README.md添加文件

git commit -m "first commit" adds the file to the master branch since that still default for git git commit -m "first commit"将文件添加到master分支,因为这仍然是 git 的默认值

so I never have to rename it in the first place.所以我一开始就不必重命名它。 Now Github uses the default main, which can be changed in settings -> repositories on github.com现在 Github 使用默认的 main,可以在 github.com 上的设置 -> 存储库中更改

Using the -m option (move/rename) instead of -M with the name of the branch you're renaming from, here main, will work.使用-m选项(移动/重命名)而不是-M与您正在重命名的分支的名称,这里是 main,将起作用。 Then you can push your renamed branch and maintain your reflog as well.然后,您可以推送重命名的分支并维护您的 reflog。

git branch -m main master
git push -u origin master

I also wrote a blog post with more details which can in case the repo was cloned by someone else before renaming the branch.我还写了一篇包含更多详细信息的博客文章,以防在重命名分支之前其他人克隆了 repo。 You can check it out here. 你可以在这里查看。

I'm not able to vote up yet ("Thanks for the feedback! You need at least 15 reputation to cast a vote, but your feedback has been recorded.").我还不能投票(“感谢您的反馈!您至少需要 15 声望才能投票,但您的反馈已被记录。”)。 I just want to call out CodingNagger's reply as working great for me, thank you!我只想指出 CodingNagger 的回复对我来说非常有用,谢谢!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM