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

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

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


当前回答

我也有同样的问题。我遇到这个问题是因为我没有做出任何承诺,甚至没有最初的承诺,但我仍然试图推动。

一旦我真的commit -m“your msg”,一切都很正常。

其他回答

我按照下面的步骤,它对我很有效。

 rm -rf .git
 git init
 git add .
 git commit -m"first message"
 git remote add origin "LINK"
 git push -u origin master

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

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

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

我的答案不是针对这个特定的问题,但可能会帮助那些有同样错误的人。 如果你在做加法。在一个空目录上提交和推,你可能会得到同样的错误。在构建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

GitHub将默认分支名从master改为main。因此,如果您最近创建了回购,请尝试推主分支。

git push origin main

这是初学者容易犯的错误。

从master重命名默认分支。

(注意:从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时的默认选项。