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 its contents. When he was informed of the vegetables it contained, he said, "Bring it close to one of his companions."When he saw that the Prophet disliked eating it, he too declined. The Prophet ﷺ said to him, "Eat, for I converse privately with one with whom you do not converse."
أن رسول الله ﷺ قال: من أكل ثوما أو بصلا فليعتزلنا- أو ليعتزل مسجدنا- وليقعد في بيته وأنه أتي بقدر فيه خضرات من بقول، فوجد لها ريحا، فسأل فأخبر بما فيها من البقول، فقال: قربوها إلى بعض أصحابه، فلما رآه كره أكلها قال: كل، فإني أناجي من لا تناجي.
Bukhari (855) and Muslim (73:564) narrated it, and this wording is his, from the route of Ibn Wahb, who said, Yunus told me, from Ibn Shihab, who said, Ata bin Abu Rabah told me, that Jabir bin Abdullah said, and he mentioned it.