At first pay attention that ahadith like that mentions to dignity of masjid;
as Imam sadiq (A.S.)said:"masajid are worthy because they are houses of Allah."(Elalol Sharaye by Sheikh Sadoogh;volume2,page318)
So going to this places has some mores such as removing bad smells.It It 'snot just about garlic,but but It is about every thing that cause bad smell in mouth like:onion,leek,etc...
Imam sadiqSadiq (A.S):"eating from garlic,onion onion,leek leek as raw or cooked doesn't have any problem,but but people who eat from them should remove Its smell before going to masjid"(sahih muslim,volume5,page49)
All foods which make bad smell and bother other muslims have the same rule. And going to masjid with bad smell,like like smell of garlic,is is mukrooh.