Search code examples
gitgithubmergegit-mergegit-revert

How do I git revert to a point at which my fork is both behind and ahead of the upstream?


I was working on a fork of a github project and wanted to incorporate the changes on it into my fork, so i could open a pull request. However I made some mistakes during the merge and ended up reverting to the previous merge. Git now says my fork is ahead of the original project, which it is not entirely. I have made another branch, which is also still at my latest commit before the merge, github says it is some commits behind my master branch. Is there any way to get back to a stage where my master branch is both behind and ahead of the original project, so I can attempt merging them again?

I have tried multiple things, like manually replacing my repository's contents with older commits, different combinations of merges between my branches, but to no avail.


Solution

  • Use git rebase instead of merging the original project.

    Explanation, as far as I understand it (A, B, C, D... are commits.)

    Original project:

    A - B - C - D
    

    Your fork immediately after forking:

    A - B - C - D
    

    Original project which has been worked on in the meantime:

    A - B - C - D - E
    

    Your fork after your feature edit (your master at that point):

    A - B - C - D - F
    

    What you wanted to have in the original project:

    A - B - C - D - E - F - (M1)
    

    where (M1) is a merge commit for merging your changes.

    What you probably have now:

    Your second branch:

    A - B - C - D - F
    

    Your master branch (probably):

    A - B - C - D - F - E - (M2) - (M3)
    

    where (M2) is you merging and (M3) reverting M2.

    What you now want is to rebase your commit F onto A-B-C-D-E, which will produce a slightly different commit F' (with a different time stamp) and a tree A-B-C-D-E-F', which can then be merged in the original project.