I encountered today a situation where a rebase failed even though the target branch is a direct ancestor of the branch I am trying to rebase.
I am currently in branch changes-2021
and trying to rebase onto master
:
$ git merge-base changes-2021 master
e335d3ebd223ad21cd53b2cf3f16876f01fc1aef
$ git show e335d3ebd223ad21cd53b2cf3f16876f01fc1aef
commit e335d3ebd223ad21cd53b2cf3f16876f01fc1aef (origin/master, origin/HEAD, master)
...
(so master is a direct ancestor)
$ git rebase master
...
CONFLICT (content): Merge conflict in frontend/src/style.scss
error: Failed to merge in the changes
...
How is that possible? I was expecting the rebase to be entirely uneventful like every other time I've encountered a similar situation in the past. The only difference this time is that the branch I am trying to rebase onto is pointing to a rather old commit (~ 5 months ago) but it's still a direct ancestor of the current branch.
CodePudding user response:
When doing a regular rebase, in the case where the target branch is a direct ancestor of the branch you wish to rebase, one of two things should happen:
- If your history is linear after the target branch, you will receive a message similar to, "Branch is up to date." This is because a rebase with the default options would have no effect, so it doesn't even attempt it.
- If your history is not linear after the target branch, meaning there is at least one new merge commit, it will attempt to replay all of the commits not reachable by the target except for the merge commits.
Since the rebase was attempted, we know you are in situation #2 (and this was also confirmed in the comments). As for why there are conflicts, this is simply the same reason any merge might have conflicts. Possibly one of the merge commits on your branch (later than the target branch) had a conflict when it was originally created, so, when the commits on either side of that merge are attempted to be rewritten linearly, the same conflict(s) will occur again. Note that since the commits are replayed one by one in a rebase, the likelihood of conflicts in this case is actually higher than if you elected to rewrite the merge commits too, by using the --rebase-merges
option.