user3933694
user3933694

Reputation: 1

Reverting a merge in GIT and then re-merging again after making a fix

I have two parallel projects with two branches A and B.

Each branch was supposed to be developed separately and merged later when the development is complete.

By mistake, I merged branch B twice into branch A prematurely. After the merge, there were more commits made to both branches.

The merge contains some new files that have been created to branch B, and there are also few files that are shared in both branch A and branch B which have been modified by branch B.

What I want to do is, revert the merge that happened in branch A, and then continue the development of branch A and branch B (make more commits to both branches after the revert), and then be able to merge these branches again at a later point.

I did some research here and came up with the following scenario. Can someone more familiar with git advise if this is a good way to go about what I want to achieve?

Let's say the merge commit is: 123

git revert -m 1 123

(commit all deletions into branch A as a result of the revert)

git checkout -b C

git revert 123

git merge B

Does this sound like the right way to approach this problem?

Any help would be appreciated. Thanks in advance.

Upvotes: 0

Views: 2046

Answers (1)

Trudbert
Trudbert

Reputation: 3198

A merge is basically just a commit with two parents that lives on one of the merged branches. That being said one of the branches (B in your case) is never changed while the merges are just commits on branch A.

So yes you can just revert them an continue working as if nothing happend (assuming the commits are recent enough that the revert produces nice results).

You can later on merge them without reverting the revert because the new merge will include the old merge (don't know if that sentence made sense).

So your approach would be

  • checkout dummy branch
  • revert merges
  • check if it broke something
  • merge reverted dummy branch back

Also there is now a chapter on undoing merges with strategies on this (I have just skimmed it)

Upvotes: 1

Related Questions