Git rebase without re-adding dropped commits

53 Views Asked by At

I have two branches:

Branch1:  (old) A B C D E F G H (new)
Branch2:  (old) A B   D E F     (new)

Notice that the C was dropped from Branch 2 and then G & H were added to Branch1.

I want to rebase Branch1 on Branch2, but I don't want the C to re-appear since it was dropped from Branch2. I only want to fast forward pushes to Branch1 from Branch2. C must be gone from history.

My broken approach:

git checkout Branch1
git rebase Branch2

Branch1:  (old) A B D E F C G H (new)

What doesn't work:

  • I don't have exact commits, I am automating the whole thing, so I can't use
    • git rebase --onto Branch2 F H
    • git cherry-pick G H
  • I only have two branch names, that's all.

I am mainly looking for some way to tell git rebase to only pick fast-forwarding commits. Is it possible?

1

There are 1 best solutions below

0
LeGEC On

One way would be to somehow keep track of the point in the history of Branch1 which was reviewed and either integrated or discarded, and then use it to run git rebase --onto Branch2 <that_point> Branch1.


one example could be to keep a reference to that commit, for example

  • a branch: git branch integration/Branch1 <F> (meaning "commit F on Branch1")
  • or a ref for which you choose a name:
    git update-ref --create-reflog -m "<some message for your reflog>" \
           refs/integration/Branch1 <F>
    

when you integrate new commits on Branch2, you should update that reference. Say you include commit G on Branch2:

  • git branch -f integration/Branch1 G
  • or:
    git update-ref --create-reflog -m "<some message for your reflog>" \
           refs/integration/Branch1 <G>
    

when you want to rebase Branch1 (or try to, at least), you can use that name:

git rebase --onto Branch2 integration/Branch1 Branch1

if this succeeds, you should then update the "integration" reference:

git branch -f integration/Branch1 Branch2

or

git update-ref --create-reflog -m "<some message for your reflog>" \
    refs/integration/Branch1 Branch2