简体   繁体   English

Git master 分支没有上游分支

[英]Git master branch has no upstream branch

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 密钥。

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), origin代表远程名称(默认为 origin),
master is the branch you want to push, in your case it's master, otherwise you would have to change that in the command. master是您要推送的分支,在您的情况下它是 master,否则您必须在命令中更改它。
-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). -u意味着,您的本地分支将被设置为跟踪源存储库上新创建的主分支(源上的主分支将是本地分支的上游分支)。 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).如果远程存储库中不存在分支 master,则将创建它,否则将对其进行更新( -u无论是否存在都有效)。

以下命令对我有用:

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

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希望它可以帮助您和其他人

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

Some people coming to this page may simply have this error because they did git push origin and 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 master a reference is added to .git\\config to link the local and remote branches.如果你做git branch --set-upstream-to=origin/master master一个引用被添加到 .git\\config 以链接本地和远程分支。 I presume that then you no longer need to specify the branch name when pushing to the remote.我认为当推送到远程时,您不再需要指定分支名称。

Create the repo on github;在github上创建repo; add a README file on github and then clone the github repository.在 github 上添加一个 README 文件,然后克隆 github 存储库。 Creating the README file (or any file actually) is needed in order to get a master branch.为了获得主分支,需要创建 README 文件(或任何实际的文件)。

Notice how github prompts for creating a README when creating a repository:请注意 github 在创建存储库时如何提示创建 README:在此处输入图片说明

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 (eg '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”命令可以正常工作。

如果有人仍然遇到这个问题,这个命令为我解决了

git push --set-upstream origin master

Try both the HTTP and SSH urls?尝试同时使用 HTTP 和 SSH 网址? I had a problem when I was using the SSH url but, when I switched to the HTTP one, it worked like a charm.我在使用 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

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指示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM