Search code examples
gitgit-revert

Two commits reverted from main (from dev), and then after merging dev to main again those reverted changes are not in main


There were two commits which should not go to main, but we merged to main from dev and then we reverted those changes. Meanwhile we had some additional changes in dev and then at later date we merged dev to main again. However, those two commits that we reverted are not in main after merge has been completed from dev to main.

Need help to get the changes from the two commits to main branch.


Solution

  • This happens because the commit IDs of those commits already exist in main, so they can not be brought in again. You generally have 3 options:

    1. Revert the revert(s) of those commits. This can be confusing in the history so if you choose this route I recommend that you add details to the commit message explaining why you are doing this.
    2. Rewrite the commit IDs of the commits that were reverted and need to be brought in again. The easiest way to do it is to git rebase --no-ff commit-X where commit-X is the parent of the first commit to rewrite. However, if their are other commits after those you wish to rewrite, you may not want to re-write the entire branch. In that case create a new branch off of the target branch (main), cherry-pick the desired commits (which will change the IDs), and then merge them into the target (main).
    3. Reset your branch backwards to before the revert. You rarely ever want to do this on a shared branch such as main. I only mention it in "general", but I highly doubt it is a viable option in your case.