When my 18 month old does the same thing I punish her. You have to figure out what works for him. She gets a pat on the hand or butt and a time out depending on the severeity of her actions.
He is copying what you or someone else is saying, thats what children do they learn from us, no punishing , the answer is not to feed into it if you react to the behavior it enforces it , next time he starts to yell no or go to hit you say nothing walk away, if you dont react it will stop...
When my 21 month old daughter throws things, misbehaves too badly, gets too sassy or refuses to do something I told her to do, she gets a time out and has to stand in the corner. Of course, the punishment has to fit the age and act, so she only goes in the corner for a few seconds. But a few seconds is all she needs! She immediately says sorry and picks up whatever she threw, or does whatever I wanted.
But I agree with Darkestlight, you have to find what works with him. Dont smile or yell or give him too much of a reaction. Just punish him and stay firm. He has to understand hitting is wrong and will absolutely not be tolerated by mommy.
Try not to yell back at him as he is reacting to that, hitting a child means they will copy and do it to others,keep trying the time out way,a lot of parents do it with success it will work better as he gets older, if he comes out of time out ,no yelling ,no words quietly take him back then walk away,he can only hit you in the face or head if you are near to him, when he starts it firmly and quietly say No! then put him in time out, a chair in a corner of the room or something similar..