Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git branch behind remote after rebase

Tags:

git

rebase

I'm getting in problems with git. I wanted to rebase a branch hotfix over master local and remote, both, so this is what I did.

git checkout hotfix
git pull hotfix              #just getting someone changes
git fetch origin master
git rebase origin/master

After a long time resolving conflicts and rebuilding project I have it working ok, so I have to upload the changes to the remote branch.

git push origin hotfix

And this is waht git thinks:

$ git push origin login2
To https://[email protected]/***/***.git
 ! [rejected]        hotfix -> hotfix (non-fast-forward)
error: failed to push some refs to 'https://[email protected]/***/***.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.

I have tried:

git pull --rebase

But this gives same conflicts like before the other rebase and it shoudln't.

I Know I always can do:

git push -f

But I would preffer to avoid the forced push because I work with another person. I would like to know what I did wrong. I thought that was the way.

PD. I have run gitk and it seems there is no reference to the remote origin/hotfix, just the local hotfix appears.

Thank you!

Edit: I guess that when I did the rebase the last commit of the branch change to apply the rebase changes, that was the reason why it had another sha1 commit id. I did a pull overriding my local files.

git pull --strategy=ours origin hotfix
like image 519
Gonzalo Avatar asked Dec 29 '15 00:12

Gonzalo


1 Answers

One thing you need to know about rebase is:

Git will change the SHA hashes of every commit not part of the new base branch.

This means there will always be a conflict with the upstream branch after you rebase your local branch and the only way around it is to force push to your upstream.

A common practice is to not rebase commits that have been pushed to upstream. Rebasing is still okay if there is only one person working on a branch.

In this case, you don't have an option but to do a force push. In the future it is advisable to keep the working branch for you and your collaborator separate and have a mix of rebase and merge strategies to share your work

like image 99
TheGeorgeous Avatar answered Sep 28 '22 16:09

TheGeorgeous