如何将git中当前分支从master更改为main

11

将主分支从旧模式master更改为main是很常见的。这可以轻松地远程完成(GitHub提供了图形化的方法),但是...工作副本应该怎么办呢?


https://www.biteinteractive.com/of-git-and-github-master-and-main/ - matt
3个回答

22
  1. rename your local branch:

    git branch -m master main
    
  2. change the tracked branch

    git fetch -p origin
    git branch -u origin/main main
    
  3. change the main local branch

    git remote set-head origin -a
    
  4. optionally, remove the master branch, local and remotely:

    git branch -D master
    git push origin :master
    

更新内容

  • 感谢@torek,添加了第2步中的-p参数。
  • 添加了可选步骤以删除主分支。

1
我可能会建议在第二步使用 git fetch -p origin 或将 fetch.prune 设置为 true,但这是你想要的顺序,对吧。 - torek
我发现在远程仓库中编辑HEAD(如果您有ssh访问权限)会更改默认分支,因此在myrepo / HEAD(如果它是一个裸仓库)中将ref:refs / heads / master更改为ref:refs / heads / main,然后默认情况下克隆存储库时使用main。 - Johannes Thoma
1
除非您已经在远程源上有一个主分支,否则这将无法工作,因此它对于该问题是正确的,但不适用于在本地启动名称更改的情况。 - Peter H. Boling

1
我不得不使用
git branch -u origin/master main
而不是
git branch -u origin/main main

0
git branch --unset-upstream

之后

git branch -m master main

否则,我会得到以下错误:
致命错误:所请求的上游分支 'origin/main' 不存在

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接