0

My question is similar to Rebase feature branch onto another feature branch

While working on a feature, we mistakenly made a commit in "Feature 1" branch Commit C which was reverted and create a branch for the sub-feature and cherry picked the commit.

A - B - C - C"(Revert C) - D    -- Feature-1
     \ 
      C'(Cherry Pick C) - E - F - G   -- Branch-1

Now few bug fixes were merged onto Feature 1 which are needed for my branch to pass the tests. Hence I want to rebase Branch 1 so that it looks like

A - B - C - C"(Revert C) - D    -- Feature-1
                            \ 
                             C'(Cherry Pick C) - E - F - G   -- Branch-1

I tried to rebase the using (On Branch-1)$ git rebase Feature-1 but this did not pick the commit C' as it finds it is already present in the branch.
How should I do the rebase ?

Pratham
  • 1,182
  • 1
  • 13
  • 25
  • Do you like to remove C'? Or C, or both? – Julian Aug 09 '19 at 12:46
  • @Julian: Not remove C' but keep it. So when rebasing it finds C' commit is already present in path as commit C. Hence after rebase the C' commit does not get picked. Hence the Branch-1 looks like ` - D - E - F - G` – Pratham Aug 09 '19 at 12:49
  • Clear. I think you could cherry pick C again – Julian Aug 09 '19 at 12:54
  • In the first commit graph. Are you sure it is C' and not just C? Any reason not to reorder both branches and don't do a revert on C in Feature 1? – BlackEye Aug 09 '19 at 13:20
  • @BlackEye: `C'` is exactly same as `C`, only it is cherry picked onto a different branch. How can I reorder both branches ? Problem was we realized very late that commit C was added onto feature branch – Pratham Aug 12 '19 at 06:57
  • @Pratham you can do a rebase on any commit, so you can also can do a rebase an commit B or C for both branches and just remove commits C and C'' for feature-1 for example. – BlackEye Aug 12 '19 at 10:27
  • @BlackEye : Correct, but other dev's working on the branch would have to force pull the branch as it will not be a fast forward. Hence looking for a non intrusive solution. – Pratham Aug 12 '19 at 13:10
  • It's a feature branch, we prefer a clear and non hazardous feature branch over a non "intrusive" one. – BlackEye Aug 12 '19 at 17:37

1 Answers1

1

If you want to keep the commits in the same order, you could checkout commit D, cherry-pick commit C, then jump back to Branch-1 and rebase on top of the newly cherry-picked commit C. There very well may be a more efficient method, but this is one way.

  1. git checkout <hash of D>
  2. git cherry-pick <hash of C>
  3. git checkout Branch-1
  4. git rebase <hash of newly cherry-picked C>

The interim tree between steps 2 and 4 would look like this.

                             C'''(Cherry Pick C) (rebase on this)
                            /
A - B - C - C"(Revert C) - D    -- Feature-1
                            \ 
                             C'(Cherry Pick C) - E - F - G   -- Branch-1

You could also create temporary branches instead of using hashes, if that is easier to think about.

Bill
  • 1,265
  • 8
  • 12