我昨天还能推,现在却推不动了。

当我使用git push origin master时,我得到一个错误:

$ git remote -v
origin  https://github.com/REDACTED.git (fetch)
origin  https://github.com/REDACTED.git (push)

$ git push origin master
Username for 'https://github.com': REDACTED
Password for 'https://REDACTED@github.com':
To https://github.com/REDACTED.git
! [rejected]         master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/REDACTED.git'
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.

我的工作目录和远程存储库看起来像什么:


当前回答

检查一下你的网络连接是否正常,速度是否快。

我试图用我的4G移动热点推送,但在近10分钟内出现了这个错误。

附注:在印度,我们的4G网络只有3G速度,所以在做一些花哨的事情之前;看看是否有合理的速度可用:)

其他回答

试着用

git status

你的分支是基于“origin/main”的,但是上游已经没有了。

然后执行以下步骤

git branch --unset-upstream
git push --set-upstream origin main

推送步骤:

git init
git add README.md      -------- or --------   git add .
git commit -m "first commit"
git remote add origin 'http github link'
git push -u origin main       ------ or -------    git push -f origin master

如果分支在main上,则:

git push -u origin main

对于主人:

git push -f origin master

Try:

git push -f origin master

这样问题就解决了。

根据@Mehdi的评论,关于-force push有一个澄清:上面的Git命令只在第一次提交时安全工作。如果之前已经有提交,拉请求或分支,这将重置所有它,并将其从零设置。如果是这样,请参考@VonC的详细回答,以获得更好的解决方案。

当您没有任何文件时,可能会发生这种情况。 尝试创建一个文本文件,然后执行以下命令:

git add .
git commit -m "first commit"
git push --set-upstream origin master

在我的例子中,这两行就解决了问题。

git add .
git commit -m "Changes"

实际上,我忘记添加并提交我的更改,只是第一次尝试推送它。

git init
git remote add origin https://github.com/anything/repo-name.git
git add .
git commit -m "Changes"
git branch -M main
git push -u origin main

希望这能有所帮助!