git_解决'error: the requested upstream branch 'origin/main' does not exist'
错误详情
$ git branch --set-upstream-to=origin/main main
error: the requested upstream branch 'origin/main' does not exist
hint:
hint: If you are planning on basing your work on an upstream
hint: branch that already exists at the remote, you may need to
hint: run "git fetch" to retrieve it.
hint:
hint: If you are planning to push out a new local branch that
hint: will track its remote counterpart, you may want to use
hint: "git push -u" to set the upstream config as you push.
报错背景
1 我在一个已经存在文件的工作区执行git init
初始化git仓库,并打算上传这些文件到指定的远程仓库中(远程仓库已有其它文件)。
2 git add .
上传文件,git commit -m
3 执行git branch
,打印master,本地分支master已被创建
4 执行git branch -m master main
,重命名本地分支为main
5 执行git remote add origin xxx
添加远程仓库,执行git branch -r
无法读取远程仓库分支。
6 在存在以上问题的情况下,执行git branch --set-upstream-to=origin/main main
意图链接本地分支与上游分支,报错如上。
解决方法
1 执行命令
git pull origin main --allow-unrelated-histories
执行该命令后,git branch -r
查看远程分支打印origin/main
2 再次关联上游分支
显示成功
Branch 'main' set up to track remote branch 'main' from 'origin'.
3 pull远程库
git pull --rebase origin main
(本地同名文件与远程库同名文件一致的情况下,如果存在同名文件不一致会报错)
4 push到远程库
git push
参考链接
解决“error: the requested upstream branch ‘origin/master‘ does not exist”
解决办法:git错误 error: failed to push some refs to 'https://github.com/...