Search code examples
gitgit-mergegitversion

Revert commit on branch A which is going to be merged to B later. Commit should stay on B


Let's say I made a commit on invalid branch A, which was supposed to be made on branch B.
Commit was pushed and I found out about the mistake later.

I know that branch A will be soon merged to B and I want to only revert the commit on A and apply the commit on B.
Branch B will be at some point merged to A, but much later - as for now it's necessary to apply the change only on B.

What is a correct approach here, when I want to avoid another revert on branch B with the merge? You could assume that merge will be conducted by another person, which can easily make a mistake and just apply the revert on B.


Solution

  • After some thought, I think I found the solution.
    Let's name the invalid commit: X

    A-commits: ... Ya, X, revert_X, Za, ...
    B-commits: ... Yb, X, Zb, ...

    To avoid applying revert_X on B one must create a additional PR with commits revertX and X on branch B:

    git checkout B
    git cherry-pick revert_X
    git cherry-pick X
    

    Now on merge A -> B the revert_X is not applied, because it was already made on B.


    Sample:

    Branch A:
    Branch A files Branch B:
    Branch B files

    And commit history:

    commit-history