In merging my changes against an upstream master, I frequently find myself doing the following:
git checkout somefeature
git checkout -b integration
git rebase master # resolving conflicts along the way
git checkout somefeature
git merge integration # or rebase, doesn't matter in this example
I’ll often find that merging the integration branch back into my feature branch fails do to some conflicts. The first question I have is, “why is this happening if my integration branch is a descendent of somefeature and I’ve already resolved conflicts against the upstream master?”
If you’re wondering why I’m using an integration branch to begin with, it’s to prevent polluting my current branch with a half-failed merge.
My current workaround is to do this:
git checkout integration
git branch -f somefeature # overwrite the branch
The problem now is that I can’t push my changes back to a remote branch:
git push origin somefeature
! [rejected] somefeature -> somefeature (non-fast forward)
So now I have to remove the remote branch and re-push my changes. This can’t be the optimal way to do this, so I’m wondering, “what’s the best way to overwrite a branch and push the changes to a remote branch?”
The problem is caused because
git rebasegenerates a new series of commits that aren’t descended from thesomefeaturebranch, then when you try and merge them back intosomefeaturethe conflict resolution done during the rebase doesn’t apply. If you were to just merge instead of rebase then this would work as the merge commit would descend from thesomefeaturebranch.In terms of pushing to the remote branch you can just use
--forceto make the push succeed, that will cause problems for anyone else that has a copy of it though.