我试图推动我的项目(所有文件在一个新的存储库)。我遵循这些步骤,但当我用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.

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


当前回答

这是因为你在master中做了一些更改,所以项目要求你先拉。如果你想推它,你可以输入以下命令使用蛮力:

git push -f origin master

记得先提交你的修改:

git add .
git commit -m "Your commit message"

其他回答

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

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

我在一台开发机器上遇到了这个问题。开发部门进展不错,但是 主分支给了我(当git在开发分支上时):

! [rejected]        master -> master (non-fast-forward)

所以我试着:

git checkout master
git pull

这给了我:

You asked me to pull without telling me which branch you
want to merge with, and 'branch.master.merge' in
your configuration file does not tell me, either.

我发现主分支从.git/config中丢失了,并添加了:

[branch "master"]
    remote = origin
    merge = refs/heads/master

后来git push也在dev分支上工作得很好。

如果git pull没有帮助,那么可能你已经推送了你的更改(A),然后使用git commit -amend来添加更多的更改(B)。因此,git认为你可以丢失历史记录-它将B解释为不同的提交,尽管它包含了来自A的所有更改。

             B
            /
        ---X---A

如果A之后没人改变回购,你可以使用git push -force。

但是,如果A后面有其他人的变化:

             B
            /
        ---X---A---C

那么你必须将人从A变到B (C->D)。

             B---D
            /
        ---X---A---C

或者手动修复问题。我还没想过该怎么做。

警告:

去“git拉”并不总是一个解决方案,所以要小心。如果您有意更改了存储库历史,您可能会面临这个问题(在Q中提到的问题)。在这种情况下,git会混淆远程回购中的历史更改和新更改。所以,你应该使用git push -force,因为调用git pull会撤销你故意对历史记录所做的所有更改。

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

这对我很管用。

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