My team is moving from a git merge workflow to a rebase workflow, and I'm not understanding what I'm doing wrong. I'm working largely from this blog post as a template. Ultimately, the desire is to be able to submit a github pull request with the commits from my branch that will have no merge conflicts with master. It'd be great if that pull request could only contain my commits (for code review purposes).
The scenario is as follows:
I rebase from master into my local branch with:
git fetch
git rebase origin/master
I take care of any merge conflicts, then continue the rebasing with
git mergetool
git rebase --continue
Since I'm the only person working on my branch, I want to be able to just push my current state with
git push origin myBranch
However, when I try to push, I get the following
$ git push origin myBranch
To <github address>
! [rejected] myBranch -> myBranch (non-fast-forward)
error: failed to push some refs to <github address>
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.
git rebase origin/myBranch
I get many conflicts, because the commits that I've pulled from master are complaining about commits that existed in my branch from a previous rebase (but that I never personally changed).If you don't want to force push (push -f
) your feature branch, you'll have to avoid rebasing your feature branch after you've already pushed it to GitHub.
I use this workflow without getting the error you mention in step 6.
git checkout master
git pull
git checkout -b feature_branch
git commit -m "Finish my work"
git checkout master
git pull
git checkout feature_branch
git rebase master
git push origin feature_branch
As mipadi says in his answer, you'll have to force push your feature branch if you do the following while you have your feature branch checked out:
git push origin feature_branch
git rebase master
git push origin feature_branch
# error
git push -f origin feature_branch
The last command — git push -f origin feature_branch
— should not cause any problems if you're the only one working on that branch.
You have to force the push, using git push -f
. By default, git push
only allows a fast-forward merge (that is, it only allows you to push a branch if the new commits are direct descendants of the commits already in the remote repo). git rebase
rewrites history, which changes all the commits on the branch, so you have to force the push instead.
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