Hitting is so hard; it's especially difficult if you're a parent who doesn't hit, where is he getting it from? But hitting is fixable. So, what do you do when he hits? The gentle approach to this situation is to take his hand and ask him to look at you and use a phrase to break the 'spell'. I tend to say, "hitting hurts, I'm not going to let you hit". Sometimes, this may be enough. Othertimes it won't be. If he continues to hit, you can say, "it looks like you're having some trouble not hitting, do you need to spend some time with me to work on your feelings?". Now, this may lead to tears and that's awesome. What you need to do is to take him to a space where he can be quiet, hold him gently on your lap, not as a punishment but as a way of helping him to manage his feelings and keep him, and you, safe. If he cries, that's what he needs. The gentle parenting experts suggest hitting is a symptom of a frustration and can be helped with crying and raging. Remember, anger is not a negative emotion, it's about how he expresses his anger and his frustration. Let him get his feelings out and it'll be much easier for him to manage his frustrations.
It's also important to give him some one-on-one time. It's part of the process that will give him the attention he's likely craving and is showing you with negative acting-out by hitting.