錯誤詳情
$ 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/...