I'll try to piece together the commands.
git checkout feature
git pull --rebase origin develop
- fix conflicts, finish rebase
git push
At this point you get something like this.
To github.com:org/repo.git
! [rejected] feature -> feature (non-fast-forward)
error: failed to push some refs to 'git@github.com:org/repo.git'
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.
The hint is wrong, a git pull
here is the wrong thing. Instead you should git push --force
to overwrite the upstream feature
with your own. This is because you've rewritten the feature
branch and it has diverged with the upstream one.
Here's what's happened in more detail. At the start things looked something like this. Note that the upstream origin/feature
and your local feature
are at the same commit.
[origin/develop]
A - B - C - D - E - F [develop]
\
G - H - I [feature]
[origin/feature]
After git pull --rebase origin develop
and all the conflicts were fixed, your repository looked like this.
[origin/develop]
[develop]
A - B - C - D - E - F - G1 - H1 - I1 [feature]
\
G - H - I [origin/feature]
rebase
doesn't rewrite commits. It creates new ones and pretends it was that way all along. Now feature
and origin/feature
have diverged meaning one is not an ancestor with the other.
When you try to git push
your feature
Git refuses. It's not a simple matter of moving origin/feature
along a few commits, called a "fast-forward". A merge would be required and git push
won't do that for safety reasons. Your work appears to have diverged from everyone else's. A push
risks blowing over other people's work on the same branch.
That's why the git push --force
is necessary. It tells Git to do it anyway, put origin/feature
at commit I1
. After the git push --force
you'll have this.
[origin/develop]
[develop]
A - B - C - D - E - F - G1 - H1 - I1 [feature]
\ [origin/feature]
G - H - I
Now it's all good. Your feature
work is as if it was on top of develop
all along.
If you git pull --rebase origin develop
again, and there's no new commits on develop
, nothing should happen. If there are new commits, you'll only have to deal with those.