Praise be to Allah.
Giving voluntary charity is a confirmed Sunnah at all times, as is indicated by the Qur’aan and Sunnah and the consensus of the scholars. It says in Kashshaaf al-Qinaa‘ (2/295): Voluntary charity is mustahabb at all times according to consensus, because Allah, may He be exalted, enjoined it and encouraged it… End quote. See also the answer to question no. 36783.
What is required of the Muslim if he commits a sin is to repent to Allah from it immediately, because Allah, may He be exalted, says (interpretation of the meaning):
“And all of you beg Allaah to forgive you all, O believers, that you may be successful”
And His Messenger (blessings and peace of Allah be upon him) instructed us to repent, as he said: “O people, repent to Allaah, for I repent to Allaah one hundred times a day.” Narrated by Muslim (2702). See also the answer to question no. 129479.
If the sin had to do with the rights of another person, such as stealing or usurping property, the right must be restored to the one to whom it belongs, or his pardon and forgiveness should be sought. For more information on the other conditions of sincere repentance, please see the answer to question no. 104241.
There is nothing wrong with the Muslim giving some of his wealth in charity after every sin he commits, because of the general meaning of the verses in which Allah says (interpretation of the meaning):
“Verily, the good deeds remove the evil deeds”
“If you disclose your Sadaqaat (almsgiving), it is well; but if you conceal them and give them to the poor, that is better for you. (Allaah) will expiate you some of your sins. And Allaah is Well-Acquainted with what you do”
Ibn Jareer said: “will expiate you some of your sins” means: Allah will expiate your sins by virtue of your charity. End quote.
It was narrated that Abu Dharr (may Allah be pleased with him) said: The Messenger of Allah (blessings and peace of Allah be upon him) said to me: “Fear Allah wherever you are, and follow up a bad deed with a good deed, it will wipe it out.” Narrated by al-Tirmidhi (1987); classed as hasan by al-Albaani in Sunan al-Tirmidhi. And it was narrated that Ka‘b ibn ‘Ajrah (may Allah be pleased with him) said: The Messenger of Allah (blessings and peace of Allah be upon him) said to me: “Charity extinguishes sin as water extinguishes fire.” Al-Tirmidhi (614); classed as saheeh by al-Albaani in Sunan al-Tirmidhi.
It is proven in the story about Ka‘b ibn Maalik (may Allah be pleased with him), when he stayed behind from the campaign to Tabook, that he said: O Messenger of Allaah, as part of my repentance, I will give up my wealth as charity to Allaah and His Messenger (blessings and peace of Allah be upon him). The Messenger of Allaah (blessings and peace of Allah be upon him) said: “Keep some of your wealth; that is better for you.”
Narrated by al-Bukhaari (2758) and Muslim (2769).
Al-Nawawi (may Allah have mercy on him) said: This indicates that it is mustahabb to give charity in gratitude to Allah for ongoing blessings, especially great blessings.
End quote from Sharh Saheeh Muslim
In Mughni al-Muhtaaj (3/206) it says: It is recommended to give charity after every sin; this was stated by al-Jarjaani. End quote.
And Allah knows best.