When DD started doing this at 2 years we would give her time out by explaining what she did hurt and then placed her facing the wall for a minute and then getting her to apologise. She quickly understood that what she had done wasn't going to be tolerated and she refused to apologise for quite a while (so much so that she was nicknamed John Howard for a month or so ).

Now if she raises a hand we just have to say "do you want to stand in time out?" she will say no and stop what she is doing. She is also good at saying sorry if she accidentally hurts me as well. She has even taken herself to time out when she knows she has done something wrong which is pretty funny .