This behavior can have several causes, and it's called pet aggression. It's mostly common in young cats. If you noticed it recently, then you should take your cat to the Vet. Pet aggression can be caused by any type of discomfort or disease.
But if your cat has always done this, there are steps you should take.
So how to solve the issue of petting aggression?
There are few things that you should learn and apply.
First of all, learn to read your cat language. Rippling skin, ears back or in airplane mode, tensing up, gazing at your hand, and a fast-moving tail are the all symptoms that your cat is overstimulated and that you should stop touching them.
So if this happens every time it may be a sign that your cat doesn't like the way you pet her. Try confining petting to the head, neck, under the chin, and the chest. Short strokes help to relax the cat. Large strokes provide energy to the cat and are more likely to cause petting anger.
At first, you should pet the cat very briefly and then gradually increase the petting time. It is very important that you follow the signs your cat is giving you. If you notice that he feels uncomfortable cuddling, or that he starts wagging his tail, stop immediately. What I personally do in such cases is to put my hand next to him and not make any movements. Because you want to show the cat that you understand what it is showing you and that you have stopped doing it.
You should never use your hand as a toy when playing with your cat. When I'm fostering kittens, I have toys in my pocket so I can swiftly divert them to a toy if they start using me as a sparring partner. Play with them at least twice a day, more if they are kittens.
Never shout or act violently toward a cat who is displaying petting aggressiveness. This has the potential to exacerbate the issue significantly. Simply leave the room or move the cat to another room and ignore them for an hour or so.
If you do everything according to these tips and your cat continues to behave aggressively, then you should take her to the vet.