Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git rebase, fix conflicts, then push is rejected

When I rebase, and there are conflicts which I then resolve, I get the following message:

hint: Updates were rejected because the tip of your current branch is behind


We have 2 branches:

  • master is our base
  • feature/fix-input-height (new feature to merge into master)

I am preparing feature/fix-input-height by rebasing master to locally resolve conflicts

git checkout master
git pull origin master

git checkout feature/fix-input-height
git pull origin feature/fix-input-height

git rebase master

  • Conflicts arise
  • I resolve them
  • Then attempt to push the new feature branch
git push origin feature/fix-input-height

And end up with that rejected error message again:

hint: Updates were rejected because the tip of your current branch is behind


Everyone on stackoverflow suggests:

git push origin -f feature/fix-input-height

But forcing the push just feels wrong

like image 769
Riveascore Avatar asked May 06 '19 21:05

Riveascore


People also ask

Do I need to push again 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

Rebase works by copying commits. You start with, e.g.:

...--o--o--o   <-- master
      \
       A--B--C   <-- feature/fix-input-height (HEAD)

and end up with:

             A'-B'-C'  <-- feature/fix-input-height
            /
...--o--o--o   <-- master
      \
       A--B--C   [previous feature/fix-input-height, now abandoned]

But the other repository—the one that's not yours, that you ask your Git to git push to—still has the original commits. You don't have those commits any more. You have new and improved ones instead, but they don't know that. All they know is that you're asking them to throw away three perfectly good commits.

So they say No, I won't throw those away. Not unless you use a forceful command, rather than a polite request, at least.

That's why you need --force, or—better, though people don't use it a lot—--force-with-lease. Either way you tell them: Yes, I mean that you should throw out your commits. The difference between these two is that --force just says: Throw out your commits! Use this instead! Using --force-with-lease says: I think your feature/fix-input-height names commit C. If so: Throw out those commits! Use this instead! It will fail if someone has added commit D, that you don't have and therefore did not include in your rebase.

like image 196
torek Avatar answered Sep 17 '22 14:09

torek


Torek's answer is undoubtfully the most insightful answer here but considering your concerns and the comments you added since then, your problem is more political than technical and you're right to worry about it.

The main concern here should be « Is anybody else working on feature/fix-input-height or is it a private branch ? »

If this "feature" branch is shared, then you shouldn't rebase since somebody else would still trying to applying changes to the previous reference. If it's fully private (that's one of the purposes the branches are for), then you may move it freely.

However, to make Git happy and to avoid your co-worker to manually reset their own local copy of your branch, what I would probably do is to preserve the transitive path from the old version to the new one. That is :

  • Merge your branch on top of master, but choosing only the left side modifications when resolving a conflict, which would actually discard everything from your feature branch and leave master unchanged. But this still would introduce a merge point ;
  • Replay all commits you actually want to move on top of your new feature branch.
like image 33
Obsidian Avatar answered Sep 18 '22 14:09

Obsidian