Garlic's protective reputation is ancient and geographically enormous. It is attested in Egypt, where it was fed to workers and placed in tombs, in Greece and Rome, across the Islamic world, in India and in China. The consistency is striking and probably has a plain cause: a substance with a violent smell and a real antiseptic action, cheap and available everywhere, is going to be read as driving something off.
The vampire association is far more recent than the rest and should not be back-projected. Southeast European vampire folklore is documented from the eighteenth century onward and does include garlic among a wide range of apotropaics, alongside iron, thorn, running water and various ritual measures. What fixed garlic as the vampire repellent in the general imagination is Bram Stoker's Dracula of 1897, where Van Helsing packs the sickroom with garlic flowers, and then twentieth-century cinema, which needed a visual shorthand. A nineteenth-century novel and a twentieth-century film industry are the proximate source of a belief usually described as ancient peasant lore.
There is also a documented tradition running the other way. Several sources report garlic being avoided by those in states of ritual purity or religious discipline, it appears among substances discouraged in some Hindu and Buddhist monastic contexts as inflaming the passions, and Islamic hadith record a prohibition on entering the mosque after eating it. A plant read as protective in one frame is read as polluting in another, on essentially the same observation about its pungency.