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

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

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


当前回答

(注意:从2020年10月开始,任何新的存储库都是用默认分支main创建的,而不是master。您可以将现有存储库默认分支从master重命名为main。 2014年剩下的答案已经更新为“main”)

(以下假设github.com本身没有崩溃,eri0o在评论中指出:查看www.githubstatus.com确认)

如果GitHub回购看到了新的提交,当你在本地工作时,我建议使用:

git pull --rebase
git push

完整的语法是:

git pull --rebase origin main
git push origin main

Git 2.6+(2015年9月),在完成(一次)之后

git config --global pull.rebase true
git config --global rebase.autoStash true

简单地拉一下就够了。 (注:与Git 2.27 Q2 2020,合并。Autostash也可用于你的常规拉,没有调整)

这样,你会在最新更新的origin/main(或origin/yourBranch: git pull origin yourBranch)上重放(——rebase部分)你的本地提交。

在第6章用rebase of the Git Pocket Book中可以看到一个更完整的例子。

我建议:

# add and commit first
#
git push -u origin main

# Or git 2.37 Q2 2022+
git config --global push.autoSetupRemote true
git push

这将在本地主分支和它的上游分支之间建立跟踪关系。 在此之后,任何对该分支的未来推送都可以使用简单的:

git push

同样,使用Git 2.37+和它的全局选项推送。autoSetupRemote,一个简单的git推送即使是第一个也会做同样的事情(即:在你的本地主分支和它的上游分支origin/main之间建立跟踪关系)。

参见“为什么我需要显式地推一个新分支?”。


因为OP已经在origin/main上重置并重做了它的提交:

git reset --mixed origin/main
git add .
git commit -m "This is a new commit for what I originally planned to be amended"
git push origin main

没必要拉,换底价。

注意:git reset——mixed origin/main也可以写成git reset origin/main,因为——mixed选项是使用git reset时的默认选项。

其他回答

在push之前,你必须添加并提交更改,或者执行git push -f origin master。

如果您正在使用git push origin master,请将其更改为git push origin main,反之亦然。

试试这个Git命令,

git push origin master –f
git push origin master --force

在我的例子中,分支名称前缀已经存在于远程,所以基本上,如果你有一个分支名称“fix”,你不能推送另一个名称为“fix/new_branch_name”的分支。

重命名分支解决了我的问题。

在我的例子中,我把分行的名字拼错了。在当地,我做了这样的事情:

Git push——set-upstream origin feature /my-feature

我的分支名称缺少a in特性。我把它更正为:

Git push——set-upstream origin feature/my-feature

一切都很顺利。