4

Referencing this excellent answer, which I read and (I think) I understood, but as far as I see does not answer my specific question (because it explains how to rebase onto a commit in a different branch, but I want to rebase on the same branch).

Situation

  • There are two branches, master and feature, feature at some point gets rebased on top of master
  • Later, for whatever reason* we want feature to "branch out" from one of the previous commits of master

(* EDIT: reason, e.g.: changes in the local feature branch broke, after rebasing it on to master, so try to find out, which commit on master broke the feature branch, in a similar way as git-bisect)

Question: How to achieve this?

Illustration

How to go from this:

                 G--H--I--J    <-- feature
                /
A--B--C--D--E--F               <-- master

...to this?

        G--H--I--J             <-- feature
       /
A--B--C--D--E--F               <-- master

Attempts so far

Attempt #1:

git checkout feature
git rebase C

Result: Current branch bug/8985-miniredis is up to date. Nothing changed. I think this result is quite logical, because for git, C is also part of the "current branch". (Although, from my point of view, the "current branch" is only G..J)

Attempt #2:

git checkout feature
git rebase --onto C feature

Result: First, rewinding head to replay your work on top of it.... And then feature simply points to C and the other commits are "lost". I don't really understand what is going on here, but again, git cannot know that feature "starts" at F, from my point of view.

Attempt #3:

git checkout feature
git rebase --onto C master..feature

Result: fatal: invalid upstream 'master..feature'

As stated in the comments of that answer, this is not possible, because git inserts the ..HEAD part itself. But surely, there must be some way to achieve this?

Question in other words:

How to achieve what git rebase --onto C master..feature would do if it was valid?

Attilio
  • 1,624
  • 1
  • 17
  • 27
  • @downvoter: care to explain what is your issue? – Attilio Dec 13 '19 at 11:53
  • 1
    Oh, now I see what you are trying to do ... and it makes absolutely no sense .. what is that "whatever" reason? I mean you can create a new branch, branching from C and then push all your commits there and then delete the old feature branch ... but you will have to deal with merge conflicts surely. But just pulling the branch back to another previous commit will not work. Maybe one of this solutions may help you: https://stackoverflow.com/questions/3810348/setting-git-parent-pointer-to-a-different-parent or https://stackoverflow.com/questions/4981052/how-to-re-parent-in-git – YesThatIsMyName Dec 13 '19 at 12:09
  • Could you expand on why it "makes absolutely no sense"? Reason I'm doing this (will edit question): e.g. I have a bunch of local commits which stopped working after I rebased them onto master; now I want to find out, which commit from master broke them (a-la `git-bisect`). Re.: "You can create a branch... and delete the old branch": well this is the manual alternative for the normal rebase operation too, no? Actually *1.a* of [this answer](https://stackoverflow.com/a/4982356/3398271) seems to be exactly what I need; I'll try it and if that is the case, this question can be closed as a dupe – Attilio Dec 13 '19 at 12:54
  • @phd: yes, see my own answer below, based on the first answer of that question :) – Attilio Dec 13 '19 at 13:42
  • Now you can remove your question as it's a duplicate. – phd Dec 13 '19 at 13:42

1 Answers1

2

First of all, thanks to @YesThatIsMyName for pointing me to the right direction.

Point 1.a. of this answer has what I need.

Using the notation from above, the right command is the following:

git rebase --onto C master feature

Attilio
  • 1,624
  • 1
  • 17
  • 27