He reported that the Prophet ﷺ said, "Whoever eats garlic or onions should keep away from us," or he said, "should keep away from our mosque, and remain in his house."The Prophet ﷺ was once brought a pot containing some greens and vegetables. He noticed an odor coming from it and asked about it, and was told what vegetables were in it. He said, "Bring it close to one of his companions who was with him." When the man saw it, he disliked eating it. The Prophet ﷺ said, "Eat, for I converse with one with whom you do not converse."
زعم أن النبي ﷺ قال: من أكل ثوما أو بصلا فليعتزلنا- أو قال: فليعتزل مسجدنا- وليقعد في بيته وأن النبي ﷺ أتى بقدر فيه خضروات من بقول فوجد لها ريحا، فسأل فأخبر بما فيها من البقول فقال: قربوها إلى بعض أصحابه كان معه، فلما رآه كره أكلها قال: كل، فإني أناجي من لا تناجي.
Bukhari narrated it in the chapter on the adhan (855), and Muslim in the chapter on mosques (73/564), both from the hadith of Ibn Wahb, from Yunus, from Ibn Shihab, who said, "Ata bin Abu Rabah told me, from Jabir bin Abdullah," and he claimed it and related something similar.He also narrated it via Yahya bin Saeed, from Ibn Jurayj, who said, "Ata told me, from Jabir, from the Prophet ﷺ, who said, 'Whoever eats from this plant, garlic' (and he said on another occasion, 'whoever eats onion, garlic, and leeks'), 'should not come near our mosque, for the angels are troubled by what troubles the children of Adam.'" This is Muslim's wording, while Bukhari's wording is abridged and includes, "I think he means only when it is raw."