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

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

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


当前回答

事实上,GitHub将master改为main让我遇到了这个问题。所以从现在开始,推到原点的解决方案是:

git push -u origin main

其他回答

它为我提供了以下几个命令行的组合:

git reset 
git remote -v
git pull --rebase
git init
git add -A
git commit -m "Add your commit"
git branch -M main
git push origin main --force

小心些而已。如果他们有一个自述文件,git重置删除他们。

在推送之前未提交初始更改也会导致问题。

Use:

git push origin {your_local_branch}:{your_remote_branch}

如果本地分支和远程分支共享相同的名称,则可以省略本地分支名称。只需使用git push {your_remote_branch}。否则它会抛出这个错误。

如果您正在使用Gerrit,这可能是由提交中不适当的Change-id引起的。尝试删除Change-Id,看看会发生什么。

重命名分支,然后推送,例如:

git branch -m new-name
git push -u new-name

这对我很管用。