0

I accidentally merged my branch, which has multiple commits itself, to a release branch. Within seconds of me merging, several other branches were merged in afterward. The git log for the release branch looks like this:

commit d942b01c89369e6be474f054a66d8cc1a0d7d59f
Merge: cd1db2c632a f2096c4685e
Author: someone else

commit 285d8d69a0abe17c2d6875aaee97003e206618f4
Merge: 4e5fae9a588 1665c1da9fd
Author: me
    updates

commit 1665c1da9fdec15a9325fa58dad25064a189f366
Author: me
    updates

commit 661aa3620ec444eabe7a251c6e9662185337fb4c
Merge: 66b06e87d85 2d58cc43674
Author: me
    updates

commit 66b06e87d85ec0c6702ad7fdee555f52bc47ca89
Author: me
    updates

commit f3162b881507609a2b7ded077f02c272e6ca7bb7
Author: me
    updates

commit 4e5fae9a58893e05f9853d24dd71f4e140f49165
Merge: 53f93040834 2d58cc43674
Author: someone else

So I can't just reset the head to 4e5fae9a58893e05f9853d24dd71f4e140f49165 (I think I'm understanding that correctly) because of the merges that came afterward. I just need to "undo" the merge that brought in the commits by me

TimaSan
  • 15
  • 1
Jerry Skidmore
  • 400
  • 2
  • 7
  • 20
  • the problem with reverting/resetting commits that you have already pushed is that others might have pulled your changes. So, if you hard reset the branch, you might end up with the situation that others will keep on developing on the state with previously pulled commit. That may lead to a big troubles and clean up. So, it depends on what you want to achieve. Clean history (you merge has never happened) vs simple clean up (subsequent commit there you have removing unwonted modifications.) – Dmitry Nov 06 '21 at 12:38
  • if you have no strict rules of what is supposed to be committed and how, I would go with a simple clean up as a subsequent commit (removing your unwanted changes). If you have strict rules and need to have a clean history. Go with reset hard. Also, for the future think how you could prevent such accidents (git hooks, etc..). Depending on the tools you have, you can have a very well shaped workflow with a lot of guards. For instance if you use bitbucket + scriptrunner. – Dmitry Nov 06 '21 at 12:42

1 Answers1

1

You can git revert -m 1 285d8d69.

Note that you will then be unable to merge any of the commits in that branch later, because although the effect of the merge will be undone, the topology of the merge will remain (and there's nothing you can do about that at this point). This is the situation discussed at length in the famous explanation at https://github.com/git/git/blob/master/Documentation/howto/revert-a-faulty-merge.txt, which provides solutions to the difficulty in case you want to perform the merge later.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • I should have mentioned that wasn't really an option...I had read that, just forgot to mention that this will likely be merged in this release branch or the subsequent, likely with some changes (from code review). I guess I will just have to have it reviewed and make changes that way or delete it, save the code, re-add it and merge soon... – Jerry Skidmore Nov 06 '21 at 02:37