Reversing Commits in Git

I had an issue this morning where I had merged two branches and immediately wished I hadn't since I wasn't done with the feature branch that I merged into. The right thing would have been to thoroughly think it though before doing the merge but we all have bad days! After spending way too much time trying various permutations of git reset with every different flag that looked even close to what I wanted I finally found a way that worked!

On this branch I had two relevant commits:

Comment SHA
The bad idea merge f04f772fefe80eecb148692f9e3190cfe08a012b
The last good commit 6e351b9b6fadcff957fb5c7965e0813636e8dd6c

I hadn't made any changes since the merge and there was no one else working on this branch so the first thing I needed to do is reset my local state to the last good commit.

git reset --hard 6e351b9b6fadcff957fb5c7965e0813636e8dd6c

Now I just needed to blow away the remote branch and create another one with the same name.
git push origin :feature/ContentViewer
git push origin +feature/ContentViewer

If I had not been the only person working on this branch I would have needed to coordinate with them to let them know that I was going to blow it away. They would have had to commit and push what they were working on so I could have made a patch for it to be applied after recreating the branch. That would have looked like this:

Comment SHA
More work commited on top of the merge 7bcfc47f7214eac135c2f426bf31a69d1f357116
The bad idea merge f04f772fefe80eecb148692f9e3190cfe08a012b
The last good commit 6e351b9b6fadcff957fb5c7965e0813636e8dd6c

Then I would have needed to make a patch for all of the changes between the bad merge and the last commit.

git format-patch --stdout f04f772fefe80eecb148692f9e3190cfe08a012b..7bcfc47f7214eac135c2f426bf31a69d1f357116 > update_patch

Then I could have reset my local state to before the merge just like before.
git reset --hard 6e351b9b6fadcff957fb5c7965e0813636e8dd6c

And then applied the patch to this state so it looks like the merge never happened.
git am update_patch

Finally just blow away and recreate the remote branch like before.
git push origin :feature/ContentViewer
git push origin +feature/ContentViewer

Then I could have let my team know that I fixed my mistake and they can start using that branch again.