Search code examples
gitgit-branchgit-commitgit-history

How to move starting point of branch to an older commit


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.


Solution

  • 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.