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

当我使用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.

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


当前回答

当远程服务器有一些额外的提交时,这个问题就会出现在你的工作目录中。下面是修复此问题的解决方案。

若要从远程服务器获取最新代码并将其推送到本地,请执行 git拉 git推 直接执行强制推送到远程服务器。 Git push—force

如果第一条行不通,那就使用第二条。

使用下面的命令获取所有与push相关的选项:

git push --help

其他回答

我尝试了'git push origin main',然后收到了消息。然后我尝试单独使用“git push”,但它不起作用。

我检查了一下我是否承诺了(是的)。我试着“拉”,然后“推”,但没有。

在开始一些特技之前,我只是关闭并打开一个新的终端,然后再次“推”,它工作了:p

我的答案不是针对这个特定的问题,但可能会帮助那些有同样错误的人。 如果你在做加法。在一个空目录上提交和推,你可能会得到同样的错误。在构建Github存储库之后,在本地目录中运行这些代码。关键在于,您需要添加/创建一个文件,并在将存储库推送到远程存储库之前提交此更改。

echo "# something" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin https://github.com/[address of your repository].git
git push -u origin main

Try:

git push -f origin master

这样问题就解决了。

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

我不确定这是否适用,但对我来说,修复是在git初始化后在本地提交一些东西。然后我使用——set-upstream推到远程。

当远程服务器有一些额外的提交时,这个问题就会出现在你的工作目录中。下面是修复此问题的解决方案。

若要从远程服务器获取最新代码并将其推送到本地,请执行 git拉 git推 直接执行强制推送到远程服务器。 Git push—force

如果第一条行不通,那就使用第二条。

使用下面的命令获取所有与push相关的选项:

git push --help