The Messenger of Allah ﷺ 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."A pot containing some green vegetables was once brought to him. He noticed an odor coming from it and asked about it, and he was told what vegetables were in it. He said, "Bring it close to one of my companions."When the companion saw it, he disliked eating from it. The Prophet ﷺ said to him, "Eat, for I converse privately with one with whom you do not converse."
أن رسول الله ﷺ قال: من أكل ثوما أو بصلا فليعتزلنا- أو ليعتزل مسجدنا- ولتقعد في بيته، وانه أتي بقدر فيه خضرات من بقول، فوجد لها ريحا، فسأل فأخبر بما فيها من البقول، فقال: قربوها إلى بعض أصحابه، فلما رآه كره أكلها، قال: كل فإني أناجي من أناجي من لا تناجي.
Bukhari reported it in the Book of Prayer (855), and Muslim in the Book of Mosques (564:73) - and the wording here is his - via Ibn Wahb, who said, Yunus told me, from Ibn Shihab, who said, Ata bin Abu Rabah told me, that Jabir bin Abdullah said, and then he mentioned it.