我试图推动我的项目(所有文件在一个新的存储库)。我遵循这些步骤,但当我用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 push之前,git拉。显然,您的本地分支与跟踪分支不同步。

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

其他回答

! master -> master(非快进)

不要惊慌,这很容易解决。你所要做的就是发出一个拉,你的分支就会快进:

$ git pull myrepo master

然后重试你的推送,一切都会好起来的:

$ git推github master

你需要做的

git branch

如果输出是这样的:

* (no branch)
master

然后做

git checkout master

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

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

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

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

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

这也可能是由于在给Repo命名时引起的一些名称错误造成的。 如果上面的任何一个答案都不起作用,下面这个对我来说是有效的:

删除该repo并创建一个新的repo,并再次尝试以下命令:

cd 'Local Directory Path'
git remote add origin *your_git_name.git*
git push -u origin master

如果add origin显示已经存在,则使用以下方法:

git remote set-url origin *your_git_name.git*

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