To avoid getting yourself into a mess in the first place:
Gradually build up your awareness and understanding of diversity concerns by reading an article or book chapter once a month about gender and diversity issues in academia.
Build up your empathy: when you hear about an incident of intolerance, imagine that you work in a female-dominated field that men (including you) have had difficulty cracking into, and someone says something that draws attention to your gender minority status.
Let's imagine you've been called out for saying something sexist or intolerant. Perhaps you showed ignorance, lack of consideration or a lapse in judgment. Perhaps someone else showed hypersensitivity or reactivity.
It's understandable that you might feel confused, hurt, indignant. Express these feelings in private, not in public. Remember that email can appear deceptively private but is actually extremely public.
Allow yourself time, before responding in a public way, to think about what happened, and imagine how the other person/people might feel. If you draft an email, don't hit "send" right away -- sleep on it; show it to someone whose opinion you value.
If anyone pushes you to apologize before you feel ready, ask for some time to reflect. An apology that criticizes the person who felt hurt is worse than no apology at all.