3

I have a branch called feature which branches off main at commit F, with two commits, A and B.

This was merged with a merge commit, M, and then the merged was reverted with commit R.

I now want to rebase branch feature so it's on the tip of main, with its commits A and B again, (so I can carry on working on it and fix the problem that caused the revert to be needed).

However, when I do git rebase main, git just fast-forwards me to the tip of main, presumably because it sees that A and B are already on main.

How do I force this?

I've tried git rebase --onto main SHA_FOR_F --no-ff but that does the same thing.

Here's the situation:

R - main | M |\ | B - feature | | | A |/ F | 

and I'd like:

B - feature | A | R - main | M |\ | B | | | A |/ F | 

I know I could just cherry-pick A and B individually, but is there a way of doing this in one go?

3
  • It's not that Git is doing a "fast-forward" with these, it's that rebase itself is detecting patch-equivalent commits in the upstream, and removing the commits from the list of commits to be copied. Commented Jun 17, 2021 at 15:40
  • Indeed, but I think the output on the terminal mentions fast-forward. Commented Jun 18, 2021 at 20:39
  • There is a kind of fast-forward option with merging, but that's to re-use existing commits. That is, suppose you git rebase --onto X while on branch B with commits Y and Z, and suppose that the actual topology is ...--X--Y--Z. Then commits Y and Z already exist as two commits immediately following X, so git rebase by default re-uses them. Adding -f or --no-ff (either one) tells rebase to copy Y and Z anyway. Rebase doesn't say anything about this at the time it does it, though. Commented Jun 18, 2021 at 20:43

1 Answer 1

4

(if you only have 2 commits, cherry-picking by hand is obviously a very viable option)

You can also pass a range to git cherry-pick :

git cherry-pick F..B 

This will skip the "detect if a commit is already part of target branch" algorithm which git rebase does.


[edit]

actually, git rebase also has an option to skip this check : --reapply-cherry-picks (added in v2.27, Q2 2020)

git rebase --reapply-cherry-picks --onto master F B 

the advantage being you can also add --interactive, and edit yourself what should and shouldn't be kept.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! (It's more than 2 commits; I was simplifying.)
Note that --reapply-cherry-picks was new in Git 2.27.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.