OK, I thought this was a simple git scenario, what am I missing?
I have a master branch and a feature branch. I do some work on master, some on feature, and then some more on master. I end up with something like this (lexicographic order implies the order of commits):
A--B--C------F--G (master)
\
D--E (feature)
I have no problem to git push origin master to keep the remote master updated, nor with git push origin feature (when on feature) to maintain a remote backup for my feature work. Up until now, we’re good.
But now I want to rebase feature on top of the F--G commits on master, so I git checkout feature and git rebase master. Still good. Now we have:
A--B--C------F--G (master)
\
D'--E' (feature)
Problem: the moment I want to backup the new rebased feature branched with git push origin feature, the push is rejected since the tree has changed due to the rebasing. This can only be solved with git push --force origin feature.
I hate using --force without being sure I need it. So, do I need it? Does the rebasing necessarily imply that the next push should be --forceful?
This feature branch is not shared with any other devs, so I have no problem de facto with the force push, I’m not going to lose any data, the question is more conceptual.
The problem is that
git pushassumes that remote branch can be fast-forwarded to your local branch, that is that all the difference between local and remote branches is in local having some new commits at the end like that:When you perform
git rebasecommits D and E are applied to new base and new commits are created. That means after rebase you have something like that:In that situation remote branch can’t be fast-forwarded to local. Though, theoretically local branch can be merged into remote (obviously you don’t need it in that case), but as
git pushperforms only fast-forward merges it throws and error.And what
--forceoption does is just ignoring state of remote branch and setting it to the commit you’re pushing into it. Sogit push --force origin feature-branchsimply overridesorigin/feature-branchwith localfeature-branch.In my opinion, rebasing feature branches on
masterand force-pushing them back to remote repository is OK as long as you’re the only one who works on that branch.