Search code examples
gitbranching-and-merginggit-revert

Revert all merged commits after pushing them to remote


I have the next case. There are two branches:

  • dev- our development branch that includes all the current results;
  • ml_DEL-1049 - my working branch which was created for the particular task. While I was working in my ml_DEL-1049 branch I merged dev into it from time to time. And finally, I merged my branch ml_DEL-1049 into the dev branch. You can see it in the pic. enter image description here

Now, I have to revert this merge. But when I try to revert the last commit (merge commit) I revert only changes from this particular commit (changes of formBlock.vue file). But not all the previous commits from my working branch. As an additional condition: I'm going to merge the results of ml_DEL-1049 to the dev branch later.

If I understand properly, I should revert merging and then when I need to merge it again I will revert the revert commit. But how to perform it? Is there any way to not search for all commits to revert in my ml_DEL-1049 branch?

This question contains two parts:

  1. How to revert it now?
  2. How to merge results to dev after reverting in the future?

Note: on the screenshot, there are commits from the dev branch filtered by the message 'DEL-1049'. All the commits have been pushed to remote and there are already many commits after and between them.

The result of git log --graph --oneline: enter image description here

*Sorry for the pictures, I can't get text data from the Amazon Workspace


Solution

  • If you are reverting just the last commit (a regular commit) of a branch, git will not revert changes from commits that are behind it. If you need to revert changes that came in as part of a merge, then you need to revert the merge commit itself (which will require you to use the -m option of revert).

    Then, about how to be able to commit that same stuff again: I think it would be much simpler to handle if you had kept a straight no-merges branch for your development because then it's very simple to rewrite history of your branch so that, to git, it's all new stuff that hasn't been merged ever... if you have merges pulling stuff from the upstream branch, then it's a little tricky and probably the easiest way to do it would be to revert the reversal, as you propose.