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分支;

        

posted @ 2021-04-17 09:52  Amerys  阅读(14269)  评论(0编辑  收藏  举报