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 --force
ful?
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.
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.
A commit gets rejected and causes a failed to push some refs to error because the remote branch contains code that you do not have locally. What this means is that your local git repository is not compatible with the remote origin. Based on the above, your local machine is missing commits C and D.
If the branch you want to force-push is protected, you can't force push to it unless you either: Unprotect it.
There is no need to do a git pull after you have rebased your feature branch on top of master . With your current workflow, the reason why git status is telling you this: Your branch and 'origin/feature' have diverged, and have 27 and 2 different commits each, respectively.
Instead of using -f
or --force
developers should use
--force-with-lease
Why? Because it checks the remote branch for changes which is absolutely a good idea. Let's imagine that James and Lisa are working on the same feature branch and Lisa has pushed a commit. James now rebases his local branch and is rejected when trying to push. Of course James thinks this is due to rebase and uses --force
and would rewrite all Lisa's changes. If James had used --force-with-lease
he would have received a warning that there are commits done by someone else. I don't see why anyone would use --force
instead of --force-with-lease
when pushing after a rebase.
The problem is that git push
assumes 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:
Z--X--R <- origin/some-branch (can be fast-forwarded to Y commit) \ T--Y <- some-branch
When you perform git rebase
commits D and E are applied to new base and new commits are created. That means after rebase you have something like that:
A--B--C------F--G--D'--E' <- feature-branch \ D--E <- origin/feature-branch
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 push
performs only fast-forward merges it throws and error.
And what --force
option does is just ignoring state of remote branch and setting it to the commit you're pushing into it. So git push --force origin feature-branch
simply overrides origin/feature-branch
with local feature-branch
.
In my opinion, rebasing feature branches on master
and force-pushing them back to remote repository is OK as long as you're the only one who works on that branch.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With