3

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.

Community
  • 1
  • 1
printfmyname
  • 983
  • 15
  • 30
  • One problem I see is that you did `git merge master` immediately _after_ doing `git rebase master`. Why did you do this? – Tim Biegeleisen Jun 19 '16 at 00:06
  • My intention was to merge changes with the master to bring my branch upto date and then ran rebase to position branch onto master. You think that may have cause all these problems? – printfmyname Jun 19 '16 at 00:17

1 Answers1

6

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.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • The changes I submitted are intended to merge with master since they should go in to the production. Would running above command, to cause git to treat my branch as personal, still throw an error when ower of the remote git tries to merge it with the master? Altarnatively, Is it possible(or too late) to delete everything on my branch, ie like revert all changes so that my branch match exactly with the remote repo? I can manually save all files and transfer changes later since the change set is not that big. – printfmyname Jun 19 '16 at 00:45
  • I think you are not understanding Git workflow as your comment does not make sense to me. – Tim Biegeleisen Jun 19 '16 at 01:28
  • If you don't have permissions to do a force push, then you probably should not be using a rebase workflow. – Tim Biegeleisen Jun 19 '16 at 01:45
  • 1
    @printfmyname, no to both your follow up questions. First, collaborators trying to merge your changes in would get easy fast forward merges. Second, it's not too late to delete everything and do it all over again, but then that would also be a whirlpool you might not escape. – Jeff Puckett Jun 19 '16 at 02:41
  • 1
    @Jaff I just did that. Everything seem to work fine. To those who with the same issue, I removed the remote branch and did a git pull. then once I rebase redactor_changes to master ( I had to resolve conflicts too), I performed a git push origin redactor_changes. Thanks Tim for a solution and visuals. I was bit worried to execute a command with --force tag, even though it could pull me to a safer land. – printfmyname Jun 19 '16 at 03:28
  • @printfmyname You are wise to be wary of using `git push --force` too liberally. Although updating a remote branch after a rebase is one of the legitimate uses of force pushing. – Tim Biegeleisen Jun 19 '16 at 03:31