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

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


当前回答

我在github创建了新的回购,我有同样的问题,但它也有问题,而拉,所以这为我工作。

但在已经有很多这样的代码的回购中,不建议这样做 会把一切都搞砸

git push origin master --force

其他回答

我通过以下步骤解决了这个问题:

git add *
git fetch
git pull origin main --allow-unrelated-histories or git pull --rebase <your_reponame> <your_branch>
git push -u "<pass your project clone url>" main or git pull origin [branch]

如果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 branch

如果输出是这样的:

* (no branch)
master

然后做

git checkout master

确保没有任何挂起的提交,因为签出会丢失所有未提交的更改。

我也有同样的问题。 我使用Git Totoise。 右击->TotoiseGit ->清理。 现在你可以推送到Github了 这对我很有效:D

注意:这绝不是git的推荐用法。这将覆盖远程上的更改。只有在100%确定本地更改应该推送到远程主服务器时才这样做。

⚠️试试这个:git push -f origin master