Los Angeles Superior Court Judge Carol Boas Goodson granted Swift a protective order against Timothy Sweet that lasts for three years.
Sweet has said in the past that he believes he is married to the US musician.
The 24-year-old singer wrote in a sworn declaration that she fears for her safety in public and at home.
She said: "Mr. Sweet's course of conduct, including threats to harm my family, are seriously alarming and harassing and are extremely distressing to me and my family."
Sweet sent messages threatening both Swift's family and anyone who interferes in the relationship he believes he has with her.
According to the court filing, one of the messages stated: "Dearest Taylor, I'll kill any man who gets in the way of our marriage."
Court documents show Sweet has sent threatening messages online and went to the singer's Beverly Hills home in February.Sweet has also gone to the singer's house in Nashville.
The restraining order covers Swift's parents, her homes and places where she works in Los Angeles and Nashville.
Sweet did not attend Tuesday's hearing and did not respond to the singer's restraining order petition.