Anas bin Malik reported,

The Companions of Muhammad ﷺ were saying on the day of the Trench, "We are the ones who pledged allegiance to Muhammad upon Islam for as long as we live," or he said, "upon jihad." Hammad was uncertain which. And the Prophet ﷺ was saying, "O Allah, the true good is the good of the Hereafter, so forgive the Ansar and the Muhajirun."

Muslim 4676

Sahih

حدثني محمد بن حاتم حدثنا بهز حدثنا حماد بن سلمة حدثنا ثابت

عن أنس

أن أصحاب محمد ﷺ كانوا يقولون يوم الخندق نحن الذين بايعوا محمدا على الإسلام ما بقينا أبدا أو قال على الجهاد شك حماد والنبي ﷺ يقول اللهم إن الخير خير الآخره فاغفر للأنصار والمهاجره

مسلم 4676صحيح
Contents
Reference
Commentary
Ayat
Collection
Sahih Muslim
Book
Jihad and expeditions
Chapter
Expedition of Ahzab, which is the Trench
Numbering
Darussalam 4676
Muslim 4676