How to undo merge keeping changes made after

Asked

Viewed 649 times

4

The branch feature had a merge of aleatorio.

How to delete this merge by keeping changes made in feature, before and after the merge?

inserir a descrição da imagem aqui

  • 1

    I believe you’ll have a manual job. Because the way I see it, I would have to go back to merge with git reset or git revert and copy the new files/changes to the repository that you unmounted.

  • 1

    This link simulates some cases, it may help you: http://dojo.objectos.com.br/caixa/git-04-resolver_merge_incorrecto.html

1 answer

1

For cases like this, I prefer to create a temporary branch, make changes to it and only at the end, I change the branch I want (in case, Feature) to point to this temporary branch.

So first we have to create the temporary branch (which I’ll call by the creative name "temp"), causing it to point to the C9 commit (which is the last commit before the merge):

git checkout -b temp C9

Obviously, you should exchange "C9" for the respective commit hash.

With the above command, the branch temp is created and I will already be in it (the contents of my directory will be the same as the C9 commit).

Then you can use git cherry-pick to apply a group of commits:

git cherry-pick C11^..C12

Thus, all commits from C11 to C12 will be applied (note ^ right after C11, so that it is included). If you had more commits (for example, from C11 to C20, just do C11^..C20).

In this case, as there are only two commits, it would also be possible to do git cherry-pick C11 C12 <- this option is also useful when commits are not in sequence (it could be a commit from each branch with no relationship between them, for example, and then the syntax C11^..C12 would not work).

In this case, the command cherry-pick checks for each commit in the list which changes are made by that commit and applies to the current branch. That is, first it takes the C11 and applies (creating another commit, but keep the same changes and the same comment). Then do the same with C12.

Now the branch temp has the same changes as the C11 and C12 commits (and does not merge with the "randomized" branch), but it is important to note that are different commits, since the commit date is not the same as the original commits (and if someone else did it, the authors will also be different - only the commit changes and comment are the same).


Now just get back to the branch Feature and make it point to the branch temp:

git checkout feature
git reset --hard temp

And finally, we erase the branch temp, because we don’t need him anymore:

git branch -d temp 

With that, the branch Feature now has the C11 and C12 commit changes. But again, it’s not exactly the same commits, because the dates aren’t the same - and the author also might not be. Only commits changes and comments are the same. Hence, the original C11 and C12 commits will be lost (if they cannot be reached by any other branch).


About the reset --hard, you can see more details in the manual and in this question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.