I read the git book but somehow forgot the rule that says:
Do not rebase commits that you have pushed to a public repository.
If you follow that guideline, you’ll be fine. If you don’t, people
will hate you, and you’ll be scorned by friends and family.
Here goes, at work I created a local branch feature-xyz which I pushed to the remote repo. I pull it on a different computer, did some more work and pushed. Back at work, I pulled the branch. After a few commits, I rebased my branch. Now I’m done with the feature-xyz and want to push it to the remote repository but this is obviously failing with the following message:
$ git push origin feature-xyz
To git@<url>:<repo>.git
! [rejected] feature-xyz -> feature-xyz (non-fast-forward)
error: failed to push some refs to 'git@<url>:<repo>.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Merge the remote changes (e.g. 'git pull')
hint: before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
Obviously pulling the remote branch creates all sorts of conflicts. So instead I decided to delete the remote branch:
git push origin :feature-xyz
and recreate it:
git push origin feature-xyz
However I was wondering if there’s a better workflow not involving deleting the remote branch?
Don’t rebase then. Merge.
Ie, don’t rebase
feature-branchon top ofanotherBranch. MergeanotherBranchintofeature-branch.Note that deleting the remote branch or
push --forcethe same branch is the same.In both case, you have published new SHA1, rewritten the history, which can be inconvenient for anyone else having previously pulled
feature-branch.