How do I find the common ancestor to rebase onto?

Viewed 531

How do I find what commit a branch originally branched from, after the upstream branch has been rebased?

I often need to rebase several related branches when I pull origin/master:

Before rebasing b1:

O1 - O2 - O3 - O4 - O5 - O6 <=master
       \
        G - H - I <=b1
                 \
                  T - U - V <=b2

After rebasing b1:

O1 - O2 - O3 - O4 - O5 - O6 <=master
       \                   \
        G - H - I           G' - H' - I' <=b1
                 \
                  T - U - V <=b2

Rebasing b1 required a lot of conflict resolutions. To avoid resolving the same conflicts when rebasing b2, how do I find what commit b2 originally branched from, i.e. I?


rebase forces me to re-resolve all the original conflicts Branch1 had:

$ git checkout b2
$ git rebase  # crap!

To avoid that, I want to do is rebase b2 onto the original commit it branched off of, i.e. I. The set of commits I want to apply is I..b2, which means I want to use I as the upstream for rebase (even though it's not a branch anymore). Now I have to specify --onto b1 (otherwise git will apply my changes onto I, which is what I already have).

git rebase --onto b1 I

My question: How do I find I?

1 Answers

You don't need to. Git can actually figure this out for you using the reflog. Just pass the --fork-point flag to git rebase, e.g. git rebase --fork-point b1.

If you do need to do this manually (e.g. you're using an old version of Git), you can just look through the reflog yourself. Either git log -g b1 or git reflog show b1 will show you the reflog for b1, and you can use that to figure out what the last commit was before the rebase.

Related