Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 582
ben39 Avatar asked Jan 20 '12 23:01

ben39


People also ask

How do I fix a non-Fast-Forward error in git?

If you do a commit in one project and then accidentally push this commit, with bypassing code review, to another project, this will fail with the error message 'non-fast forward'. To fix the problem you should check the push specification and verify that you are pushing the commit to the correct project.

Which git command pushes the local branch to remote even if it results in non-Fast-forward merge?

Git push usage Push the specified branch to , along with all of the necessary commits and internal objects. This creates a local branch in the destination repository. To prevent you from overwriting commits, Git won't let you push when it results in a non-fast-forward merge in the destination repository.

Can I push branch after rebase?

To push the changes to the branch after a rebase, you need to force push your commits using the -f or --force flag in the git push command on Git. This is because something has changed in the remote branch and the commit history is different for both the remote and your local branch.

What conflicts can occur when forcing a push after rebasing?

While this happens, conflicts may arise. These are conflicts between your code changes in the PR and other changes that got merged into the target branch. What you could do is merge the changes from the target branch into your PR branch. That will however give a lot of merge commits and isn't very clean.


2 Answers

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.

like image 120
Adam Dymitruk Avatar answered Sep 20 '22 16:09

Adam Dymitruk


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.

like image 42
prusswan Avatar answered Sep 22 '22 16:09

prusswan