我试图推动我的项目(所有文件在一个新的存储库)。我遵循这些步骤,但当我用git push -u origin master推时,我得到这个错误:

! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'git@github.com:asantoya/projectnewbies.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.

我得到这个错误很多次,不知道该怎么做。


当前回答

有时,Git无法在不丢失提交的情况下对远程存储库进行更改。简单地说,这可能是因为您的提交丢失了,或者其他人试图推送到与您相同的分支。 要解决这个问题,首先要做一个pull请求

$ git pull origin YOUR_BRANCH_NAME

Or

$ git pull --rebase origin master

其他回答

当我在开发分支和我的主分支没有最新更新时,这种情况发生在我身上。

所以当我试图从开发分支推git时,我遇到了这个错误。

我通过切换到主分支,git拉,然后回去开发分支和git推来修复它。

$ git fetch && git checkout master
$ git pull
$ git fetch && git checkout develop
$ git push

我唯一能够解决这个问题的是删除本地和git回购,并在两端重新创建相同的。目前还好。

试试这个命令:"git pull origin master"

这对我很管用。

查看这个链接:https://help.github.com/articles/dealing-with-non-fast-forward-errors

我的遥控器不与本地同步,所以这对我来说很有效

git pull --rebase

并确保当你再次拉git时,它应该说Already up to date 现在你可以推到原点了

假设你已经有git远程添加源远程存储库URL

do

git push origin master

截图说明了一切

或者你也可以这样做

Git存储(暂时存储未提交的工作) Git pull(使本地和远程同步) Git stash pop(返回你未提交的更改) git推

正如错误消息所说:在你尝试git push之前,git拉。显然,您的本地分支与跟踪分支不同步。

根据项目规则和你的工作流程,你可能还想使用git pull -rebase。