git push -u origin master 失败

3
我已经完成了R语言课程(Coursera课程)中的第二个编程作业。
之后,我将我的本地仓库与GitHub账户上的全局仓库关联,并进行了提交和push -u origin master操作。但是我遇到了以下错误:
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/RofaidaG/ProgrammingAssignment2'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我创建了一个分支并切换到主分支,然后在我的get账户中拉取了repo,但是我收到了以下消息:

 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

我该怎么办?


你能够拉取主分支吗? - Prabhakar
你确定在再次切换到主分支后执行了 git pull 命令吗?因为答案似乎与上面失败的 git push 相同。 - MarkSkayff
2个回答

1
在将代码推送到存储库之前添加许可证或自述文件,这样就会发生这种情况。

谢谢!这很有道理 :) 每个教程都应该提到,当你想要在本地开始工作,然后推送到Github时不要这样做。每次我尝试使用新的存储库时,这都会导致很多问题。 - Michel K

0
如果您的提交在专用分支上,请尝试使用以下方法:
git fetch
git checkout mybranch (you might be already on that branch)
git rebase origin/master
git push -u origin mybranch

但是,如果您的提交在本地主分支上,并且您应该在主分支上推送,则:

git checkout master
git fetch

# replay your local commits on top of origin/master
git pull --rebase
# check everything is still working

git push

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接