Git master 分支没有上游分支

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16493396/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 16:07:50  来源:igfitidea点击:

Git master branch has no upstream branch

gitgithub

提问by JShoe

I'm trying to push one of my projects to github, and I keep getting this error:

我正在尝试将我的一个项目推送到 github,但我不断收到此错误:

fatal: The current branch master has no upstream branch.

I've never seen this before. I re-initialized my git, re-added my origin, deleted and re-made the repo, and recreated my SSH key.

我以前从未见过这个。我重新初始化了我的 git,重新添加了我的来源,删除并重新制作了 repo,并重新创建了我的 SSH 密钥。

采纳答案by GoZoner

Create the repo on github; add a README file on github and then clone the github repository. Creating the README file (or any file actually) is needed in order to get a master branch.

在github上创建repo;在 github 上添加一个 README 文件,然后克隆 github 存储库。为了获得主分支,需要创建 README 文件(或任何实际的文件)。

Notice how github prompts for creating a README when creating a repository: enter image description here

请注意 github 在创建存储库时如何提示创建 README: 在此处输入图片说明

回答by dunni

Instead of creating a new repository on Github, cloning that, or reinitializing your local repository, the following command would have been sufficient:

无需在 Github 上创建新存储库、克隆它或重新初始化您的本地存储库,以下命令就足够了:

git push -u origin master

origin stands for the remote name (default is origin), and master is the branch you want to push, in your case it's master, otherwise you would have to change that in the command.
-u means, that your local branch will be setup to track the new created master branch on the origin repository (the master on the origin will be the upstream branch of your local branch). If the branch master doesn't exist on the remote repository, it will be created, otherwise it will be updated (the -u works no matter if it exists or not).

origin 代表远程名称(默认为 origin),master 是您要推送的分支,在您的情况下它是 master,否则您必须在命令中更改它。
-u 意味着,您的本地分支将被设置为跟踪源存储库上新创建的主分支(源上的主分支将是本地分支的上游分支)。如果远程存储库中不存在分支 master,则将创建它,否则将对其进行更新(-u 无论是否存在都有效)。

回答by Manoj Shrestha

The following command worked for me:

以下命令对我有用:

git branch --set-upstream-to=origin/master master

回答by Manee.O.H

i faced the same problem just tell github to use the current head branch of your local repository:

我遇到了同样的问题,只是告诉 github 使用本地存储库的当前头分支:

git push --set-upstream origin master

wish it help you and others

希望它可以帮助您和其他人

回答by haydeniv

I had this problem today on my own remote repository, not Github and realized I had not made any commits to my local repository before trying to push to the remote repository.

我今天在我自己的远程存储库上遇到了这个问题,而不是 Github,并意识到在尝试推送到远程存储库之前我没有对本地存储库进行任何提交。

git add -A
git commit
git push origin master

回答by Cool Blue

Some people coming to this page may simply have this error because they did git push originand simply didn't realise that you need to specify the remote branch name as well, as in git push origin master.

一些访问此页面的人可能只是遇到了这个错误,因为他们确实git push origin并且只是没有意识到您还需要指定远程分支名称,如git push origin master.

If you do git branch --set-upstream-to=origin/master mastera reference is added to .git\config to link the local and remote branches. I presume that then you no longer need to specify the branch name when pushing to the remote.

如果你做git branch --set-upstream-to=origin/master master一个引用添加到 .git\config 以链接本地和远程分支。我认为当推送到远程时,您不再需要指定分支名称。

回答by Nifty

Cool Blue's answer ALMOST worked for me.

Cool Blue 的回答几乎对我有用。

First I did:"git branch --set-upstream-to=origin/master master", as recommended by Cool Blue.

首先我做了:git branch --set-upstream-to=origin/master master”,正如 Cool Blue 所推荐的那样。

But still received error message:
"failed to push some refs to '' hint: Updates were rejected because the tip of your current branch is behind its remote counterpart. Integrate the remote changes (e.g. 'git pull ...') before pushing again."

但仍然收到错误消息
“未能将一些引用推送到 '' 提示:更新被拒绝,因为您当前分支的提示落后于其远程对应分支。在再次推送之前集成远程更改(例如 'git pull ...') .”

So I...did a "git push -f" command after the git branch, which worked finally worked for me.

所以我......在 git 分支之后 做了一个“ git push -f”命令,最终对我有用。

After the forced push, subsequent "git push" commands worked without issue.

强制推送后,后续的“git push”命令可以正常工作。

回答by Crt

Try both the HTTP and SSH urls? I had a problem when I was using the SSH url but, when I switched to the HTTP one, it worked like a charm.

尝试同时使用 HTTP 和 SSH 网址?我在使用 SSH url 时遇到了问题,但是当我切换到 HTTP url 时,它就像一个魅力。

Here is what I changed:

这是我改变的:

First, view the remote URL

一、查看远程URL

git remote -v 

and you get destinations back.

你会得到目的地。

git remote rm destination 

Follow this link if you need help: https://help.github.com/articles/removing-a-remote/

如果您需要帮助,点击此链接:https: //help.github.com/articles/removing-a-remote/

Then,

然后,

git remote add origin url 
git push -u origin master

回答by Santosh Pillai

You need to configure the remote first, then push.

您需要先配置遥控器,然后推送。

git remote add origin url-to-your-repo

git remote add origin url-to-your-repo

instructions

指示