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

ben39 picture ben39 · Jan 21, 2012 · Viewed 7.5k times · Source

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?

Answer

Adam Dymitruk picture Adam Dymitruk · Jan 21, 2012

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.