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

! master -> master(非快进)

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

$ git pull myrepo master

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

$ git推github master

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

git pull --rebase

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

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

do

git push origin master

截图说明了一切

或者你也可以这样做

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

我在一台开发机器上遇到了这个问题。开发部门进展不错,但是 主分支给了我(当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 branch

如果输出是这样的:

* (no branch)
master

然后做

git checkout master

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