printfmyname
printfmyname

Reputation: 971

Git rebase shows non-fast-forward

I am buried deep within a git.

How did I get there:

I am not a git expert. Please do ask me a clarification if I lack any detail. I ran following commands few days ago to push my changes to remote branch.

% git checkout master
% git pull
% git checkout redactor_changes <-- this is the branch that I made my changes 
% git commit -m "changes" # I added my changes previously
% git rebase master
% git push origin redactor_changes # pushed changes to remote branch

Not every one has the write permission to merge changes to master branch. The guy who has the write permission on remote repo told me there is a conflict and do a rebase and submit again. So today I ran following commands in exact order.

vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git add oblog/static/css/styles-custom.css
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git add oblog/static/js/redactor.upload_image.js
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git add oblog/static/images/loading.gif
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git commit -m "Throw a toast when user upload an non-image file with image extension"
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git branch
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git checkout master
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git pull
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git checkout redactor_changes
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git rebase master
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git merge master
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git branch
vagrant@vagrant-ubuntu-trusty-64:~/oblog$ git push origin redactor_changes

Its looks like this set of command sucked me into a whirlpool that I can't escape.

Symptoms:

Last line of the above code throws following error

! [rejected]        redactor_changes -> redactor_changes (non-fast-forward)
error: failed to push some refs to 'https://git.<domain>.com/web/oblog.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

So I followed advice from fellow SO

  1. https://stackoverflow.com/a/20467414/1431184
  2. https://stackoverflow.com/a/6897674/1431184

but the problem still persist.

LABLE:

When I rebase my branch with master(locally), it throws some conflicts which I then manually fix and do a rebase --continue. Rebase was successful. Then I push my branch again to origin, it showed me same error, ! [rejected].

As suggested by git and SO, I go on the pull the changes on my remote branch to my local branch ( redactor_changes). Then executed

git rebase master

which puts me back on the the LABEL. This just goes on and on.

Note that my push command work if I don't rebase it with master, but owner of the remote git would still see the conflict when he tries to merge my branch (redactor_changes) onto master.

Upvotes: 3

Views: 5420

Answers (1)

Tim Biegeleisen
Tim Biegeleisen

Reputation: 522762

Every time you rebase your redactor_changes branch on master, you are bringing the latest changes from the latter branch into the former branch. To explain by way of diagram:

master:           A -- B -- D
redactor_changes: A -- B -- C

This simple diagram assumes that you branched off master at the B commit. Since then, you have made one commit to redactor_changes (C)and someone else has made a commit to the master branch (D). Now if you run the following commands

git checkout redactor_changes
git rebase master

you will end up with the following diagram

master:           A -- B -- D
redactor_changes: A -- B -- D -- C'

Rebasing succeeded in bringing in the new commits from master but it did so by rewriting the history of your redactor_changes branch. This is a good thing (potentially), because it means the reviewer can just fast-forward the master branch with your changes (no merge commit). But it does have one side effect. You can no longer push redactor_changes to the remote. Trying the following command

git push origin redactor_changes

will give you the error message you have been seeing

! [rejected] redactor_changes -> redactor_changes (non-fast-forward) error: failed to push some refs to 'https://git..com/web/oblog.git'
hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart...

because you rewrote the history of redactor_changes Git no longer knows how to play the commits on the remote branch.

Solution:

The solution you most likely want is to force your redactor_changes branch to the remote:

git checkout redactor_changes
git rebase master
git push --force redactor_changes

Do this and the error message should go away. This assumes that redactor_changes is a personal branch and you are not sharing it with anyone.

I also noticed some other problems with your workflow, e.g. merging master into redactor_changes after already rebasing. But doing the force push should at least advance your situation.

Upvotes: 6

Related Questions