Sahih al-Bukhari 6921
Narration text is preserved in its supplied Arabic and English. Translated interfaces and reading guides do not add narration translations in other languages.
مَنْ أَحْسَنَ فِي الإِسْلاَمِ لَمْ يُؤَاخَذْ بِمَا عَمِلَ فِي الْجَاهِلِيَّةِ، وَمَنْ أَسَاءَ فِي الإِسْلاَمِ أُخِذَ بِالأَوَّلِ وَالآخِرِ "
Narrated Ibn Mas`ud:
A man said, "O Allah's Messenger (ﷺ)! Shall we be punished for what we did in the Prelslamic Period of ignorance?" The Prophet (ﷺ) said, "Whoever does good in Islam will not be punished for what he did in the Pre-lslamic Period of ignorance and whoever does evil in Islam will be punished for his former and later (bad deeds).
Full reference and source
Sahih al-Bukhari 6921 In-book : Book 88, Hadith 4 USC-MSA web (English) : Vol. 9, Book 84, Hadith 56 (deprecated)
View source on Sunnah.com (opens in a new tab)