0

I'd like to rebase my branch onto an older commit to exclude code added by the newer commits.

What I have:

A---B---C---D---E master
                 \
                  F---G---H---I---J experiment

What I want:

A---B---C---D---E master
     \
      F---G---H---I---J experiment

Say I wanted to make my experiment branch on commit B, but didn't realize I made it on E. By the time I realize, I already made a bunch of commits and they include code changes I don't want (C, D, E).

How do I move the starting point of experiment to the older commit B?

I've read this post but haven't seen anything usable regarding moving the branch to an older commit.

SwissCodeMen
  • 4,222
  • 8
  • 24
  • 34
young_souvlaki
  • 1,886
  • 4
  • 24
  • 28
  • 1
    Does this answer your question? [Change branch base](https://stackoverflow.com/questions/10853935/change-branch-base) – SwissCodeMen Jul 15 '21 at 22:07
  • 1
    Note that the "starting point" of branch `experiment` is, and remains, commit `A`. What you do when you rebase it (see [eftshift0's answer](https://stackoverflow.com/a/68400773/1256452)) is *copy* commits `F-G-H-I-J` to new and improved, different commits `F'-G'-H'-I'-J'` where `F'` builds on `B` instead of `E`, `G'` builds on `F'` instead of `F`, and so on. – torek Jul 16 '21 at 05:33
  • 1
    Commits `A-B-C-D-E` are currently on *both branches* and after you're done, `A-B` will be on both branches with `C-D-E` exclusively on one and `F'-...-J'` exclusively on the other. Branches therefore don't (usefully) have *starting points*, they only have *ending points*. That's why you have to do things like `git log B..experiment` or `git log master..experiment` to limit what `git log` shows. – torek Jul 16 '21 at 05:34

1 Answers1

3

Simple:

git rebase --onto B master experiment

That command is read like this: git rebase --onto new-base discard-revisions-in-this-history this-is-what-i-want-to-rebase.

Git actually doesn't mind if the base is newer or older than where you are (simple example that might be rather common: a backport). It's all about the changes that are introduced that are in each revision that will be rebased.

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • Thank you! After reviewing your answer and the others spotted by SO, and reading `man git rebase` more thoroughly, it seems a more accurate interpretation is `git rebase --onto commit range-start-non-inclusive range-end-inclusive`. – young_souvlaki Jul 16 '21 at 20:20
  • 1
    Well, perhaps, it depends on how you understand the concept... and as long as it is clear, I have no objection. The only thing about what you call `range-start-non-inclusive` is that it can be a spot different from `E` in your example. Suppose that master has another revision on top of E, say `K` (`experiment` diverging from E, no change). There, you could still use `master` (pointing to `K`) as `range-start-non-inclusive`. That's why I read it as `discard-revisions-in-this-history`. – eftshift0 Jul 16 '21 at 20:24
  • 1
    Very interesting point! I have to digest that further. – young_souvlaki Jul 17 '21 at 23:01
  • 1
    Sure. When I said `understand`, I meant `visualize`. I am sure you understood already. – eftshift0 Jul 18 '21 at 00:40