1

I am not sure how to fix a situation with commits that should not have been merged into the master branch. I am not well versed in git and have been mostly using the Visual Studio controls for it.

A while ago I merged a bunch of feature branches into develop branch. However, I was not supposed to do that yet. Recently, a bug in the application needed fixing. So, I branched out of develop, fixed it, and merged back into develop. Then, I merged develop into master so that I can release the bug fixes.

After doing that, I realized I had forgotten about those feature branches that aren't to be released yet and now they are in master along with the bug fixes. It's around 200 commits that need to be reverted, so I don't want to do them one by one. Once I revert them, I'd eventually need to undo the reverts as well so I can release those features.

I first tried to undo changes up to a specific commit by doing Reset > Delete Changes (--hard), but since the changes are pushed to the remote repository, they just reappear as incoming changes.

Then I looked up a way to revert multiple commits and tried reverting a range:

git revert --no-edit <first_bad_commit-id>..<last_bad_commit-id>

But I am getting the error:

error: commit xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx is a merge but no -m option was given.
fatal: revert failed

Found this post about what is -m and how to use it, but everywhere I read I just can't comprehend how to use -m and why can't I revert merges. This is probably due to my minimal interaction with git command-line.

So, I have a range containing 200 commits I don't need that are sandwiched between commits I need. How do I undo those 200 commits?

I'd also be satisfied with a solution of undoing all the latest commits up to a specific commit. It would be quick to re-add the code for bug fixes.

Lukas
  • 1,699
  • 1
  • 16
  • 49
  • 1
    Can't you just branch out from the state before you started merging in those feature branches and apply the required fixes onto that? You don't need to release from master. – PMF Nov 17 '22 at 20:31

1 Answers1

0

Are the commits consecutive? If so, you can try using the method described by @timmouskhelichvili on the hackernoon website: https://hackernoon.com/how-to-delete-commits-from-remote-in-git, where you reset the head back to one or more commits. That is: git reset --hard HEAD~x (for x consecutive commits and can be changed depending on the number of commits to be removed).

Otherwise, you can use the method that I used which is to remove the commits up to a specific hash value: git reset --hard [hash_value] (where the hash value can be found on the remote repository or local git logs).

If the commits are not consecutive: you will need to use an interactive rebase, but I have not used that and will recommend you go check out https://hackernoon.com/how-to-delete-commits-from-remote-in-git for a better explanation.

Anja
  • 1
  • 3