Here is a very easy solution that does not remove the bad commit as you asked, but reverts its effects (variation which does really remove the bad commit at the bottom):
- Move HEAD back to your main development branch
You are currently in a detached HEAD state, so first you want to go back to the tip of your main branch (let's assume that it is master):
git checkout master
- Create a new branch and check it out
git checkout -b newBranch
- Revert the bad commit
git revert badCommit
This does not remove badCommit from the history, but it adds a new commit which removes the changes introduced by badCommit.
- Test
You can then test whether this fixes your problem.
- Submit MR
If you are happy, you can push newBranch to the remote to submit a MR. Depending on whether you have write access to the remote or not, this can look different, but something along the lines of (assuming the remote is called origin):
git push -u origin newBranch
If you really want to remove that commit from the history of your new branch, you can follow the same workflow, except that for 3. you would do:
- Remove
badCommit from newBranch
git rebase -i badCommit~
(Note the ~: you are using the commit which precedes badCommit in this command).
Then replace pick by d or drop for the line corresponding to badCommit (it will be the top line) or delete that line and save.
You might have to resolve conflict(s) before being able to conclude the rebase if removing that commit creates any conflict.
If you go with this option, be very careful not to remove your bad commit from any of the branches that have already been pushed to the remote as it would create havoc for your collaborators.