git rebase master then push origin branch results in non-fast-forward error

[亡魂溺海] 提交于 2019-11-29 19:25:41

问题


I am trying on working on my featureA branch while keeping it up-to-date with the master branch.

Here is the scenario

git clone ssh://xxx/repo

git checkout -b featureA

$ git add file.txt

$ git commit -m 'adding file' 

$ git push origin featureA

meanwhile a couple new commits where pushed to origin master

git checkout master

git pull origin master

git checkout featureA

git rebase master

git push origin feature A
To ssh://xxx/repo
 ! [rejected]        featureA -> featureA (non-fast-forward)
error: failed to push some refs to 'ssh://xxx/repo'
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.

How can I rebase without forcing the server to accept it?


回答1:


You can't push after rebasing. The commits now have different SHA1s as their history is different. If the updated ref does not contain the old ref in it's ancestry, it's a potentially harmful operation and git won't allow it.

Your only alternate is to merge if you don't want to force.

Forcing is not so bad if you are working alone and don't need to have others committing to this branch.




回答2:


A short answer to your question: you can do the reverse by rebasing master against featureA (but don't push yet), and reset featureA to that point.

This is actually cherry picking the commits from master onto featureA, the downside is that you will end up with duplicate commits on two branches. It will solve your immediate problem (if that's your intention) but in the long run you should not be rebasing commits that have already been pushed to a remote branch. The best solution in your scenario is actually to merge.



来源:https://stackoverflow.com/questions/8948964/git-rebase-master-then-push-origin-branch-results-in-non-fast-forward-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!