git推送代碼報錯:fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream


情景再現

遠程新建倉庫,然后本地 git bash執行以下代碼

git init
git add .
git commit -m 'xxx'
git remote add origin https://username@xxx.com/xxx.git
git push

在執行git push的時候出現“ The current branch master has no upstream branch.”問題的原因是沒有將本地的分支與遠程倉庫的分支進行關聯,如圖

 對應的中文意思:

fatal: 當前分值 master 沒有對應的上游分支。為推送當前分支並建立與遠程上游的跟蹤,使用 git push --set-upstream origin master

  簡單來說就是本地倉庫跟遠程倉庫關聯不起來,對應不上,不知道推到哪里去。

 

原因:在默認情況下,git push時一般會上傳到origin下的master分支上,當repositorybranch過多,又沒有設置關聯時,git就會產生疑問,因為它無法判斷你的push目標。

 

解決辦法:

  1、執行上圖中 git push --set-upstream origin master 命令即可,會自動進行關聯和推送操作

  2、執行 git push -u origin master,  這個命令也是當你新建完遠程倉庫后github的提示操作,只是它把master分支改名為main分支,如下圖所示;這里的origin master即對應你執行了 git remote add origin https://username@xxx.com/xxx.git 對應倉庫的master分支;

        


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM