Although you did not specify exactly what sin you are asking about, the Prophet ﷺ informed us that three exemptions alleviate the punishment of a sin:
- Error or lack of knowledge
- Forgetfulness
- Being forced or deprived of a willful action.
This was narrated in a hadith narrated by Abu Dhar al-Ghifari and another narrated by 'Abdullah ibn 'Abbas (quoted below):
عَنِ ابْنِ عَبَّاسٍ، عَنِ النَّبِيِّ ـ صلى الله عليه وسلم ـ قَالَ: إِنَّ اللَّهَ وَضَعَ عَنْ أُمَّتِي الْخَطَأَ وَالنِّسْيَانَ وَمَا اسْتُكْرِهُوا عَلَيْهِ
It was narrated from Ibn 'Abbas that the Prophet (ﷺ) said: "Allah has forgiven my nation for mistakes and forgetfulness, and what they are forced to do."
— Sunan Ibn Majah, Book 10, Hadith 2123
First, you need to do is ask about whether an act is permissible, and the degree (permissibility could be obligatory, preferred or neutral; whereas non-permissibility could be not encouraged, not allowed except under certain pretexts, or not allowed under any circumstances, etc. — this is not a comprehensive list). For certain topics that require an in-depth knowledge, you will not be able to get a definitive answer on an online forum; a fatwa (decree) will be required from scholars.
Second, if an act is committed that falls under any of the above exemptions, still ask for Allah's forgiveness. Allah ﷻ tells us in the Qur'an Surat Al-Ahzab 33:5 that "And there is no blame upon you for that in which you have erred but only for what your hearts intended." As Noah ﷺ advised his people, one should always attribute to Allah due grandeur:
فَقُلْتُ اسْتَغْفِرُوا رَبَّكُمْ إِنَّهُ كَانَ غَفَّارًا يُرْسِلِ السَّمَاءَ عَلَيْكُم مِّدْرَارًا وَيُمْدِدْكُم بِأَمْوَالٍ وَبَنِينَ وَيَجْعَل لَّكُمْ جَنَّاتٍ وَيَجْعَل لَّكُمْ أَنْهَارًا مَّا لَكُمْ لَا تَرْجُونَ لِلَّهِ وَقَارًا
And said, 'Ask forgiveness of your Lord. Indeed, He is ever a Perpetual Forgiver. He will send [rain from] the sky upon you in [continuing] showers. And give you increase in wealth and children and provide for you gardens and provide for you rivers. What is [the matter] with you that you do not attribute to Allah [due] grandeur?'
— Surat Nuh 71:10-13
Third, one can ask for forgiveness during prayer, by offering additional prayers to Allah ﷻ, by du'a' (supplication), by giving sadaqa (charitable donations), etc. Any form of a good deed is potentially an option:
عَنْ أَبِي ذَرٍّ جُنْدَبِ بْنِ جُنَادَةَ، وَأَبِي عَبْدِ الرَّحْمَنِ مُعَاذِ بْنِ جَبَلٍ رَضِيَ اللَّهُ عَنْهُمَا، عَنْ رَسُولِ اللَّهِ صلى الله عليه و سلم قَالَ: اتَّقِ اللَّهَ حَيْثُمَا كُنْت، وَأَتْبِعْ السَّيِّئَةَ الْحَسَنَةَ تَمْحُهَا، وَخَالِقْ النَّاسَ بِخُلُقٍ حَسَنٍ
On the authority of Abu Dharr Jundub ibn Junadah, and Abu Abdur-Rahman Muadh bin Jabal (may Allah be pleased with him), that the Messenger of Allah (peace and blessings of Allah be upon him) said: "Have taqwa (fear) of Allah wherever you may be, and follow up a bad deed with a good deed which will wipe it out, and behave well towards the people.
— 40 Hadith Nawawi 18
Last, Allah ﷻ commands us in the Qur'an not to despair of his mercy, which is greater than one can imagine:
قُلْ يَا عِبَادِيَ الَّذِينَ أَسْرَفُوا عَلَىٰ أَنفُسِهِمْ لَا تَقْنَطُوا مِن رَّحْمَةِ اللَّهِ ۚ إِنَّ اللَّهَ يَغْفِرُ الذُّنُوبَ جَمِيعًا ۚ إِنَّهُ هُوَ الْغَفُورُ الرَّحِيمُ
Say, "O My servants who have transgressed against themselves [by sinning], do not despair of the mercy of Allah. Indeed, Allah forgives all sins. Indeed, it is He who is the Forgiving, the Merciful."
— Surat Az-Zumar 39:53
Allah ﷻ does not forgive association of others with Him but may forgive any sin other than that. All we need to do is ask for His forgiveness:
إِنَّ اللَّهَ لَا يَغْفِرُ أَن يُشْرَكَ بِهِ وَيَغْفِرُ مَا دُونَ ذَٰلِكَ لِمَن يَشَاءُ ۚ وَمَن يُشْرِكْ بِاللَّهِ فَقَدِ افْتَرَىٰ إِثْمًا عَظِيمًا
Indeed, Allah does not forgive association with Him, but He forgives what is less than that for whom He wills. And he who associates others with Allah has certainly fabricated a tremendous sin.
— Surat An-Nisa 4:48