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

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

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


当前回答

不幸的是,我无法用其他解决方案解决这个问题,但我的问题是我想要推送的分支名称不被远程接受。我把它改成了正确的格式,它被接受了。

它是test/testing_routes,我需要将其更改为testing_route,其中正斜杠(/)是远程不允许的。

您应该确保分支名称格式正确。

其他回答

如果你刚刚使用git init并添加了git add文件。或者类似的东西,并且添加了远程分支,它可能是你还没有在本地提交(git commit -m 'commit message')任何东西来推送到远程…我只是有这个错误,这是我的问题。

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

就我而言,我错过了修改。我只需要运行git commit -amend,然后push。它解决了这个问题。它可能会帮助以前提交过代码的人。

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

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

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

希望这能有所帮助!