Answer
This question is discussed in our Talmud and further brought down in the codes of Jewish law. As you may be aware Judaism, unlike Christianity, does not believe man must fail. If a person does a sin he has the right to repent and, according to the circumstances below, will be forgiven with a clean slate.
There are several sources in the Bible for the requirement to confess and repent: You can find them at Leviticus 5:5, 26:40, and Deut. Chapter 30. The statement that a person’s repentance wipes his slate clean is explicitly stated in Ezekiel 33:12.
The general requirements of “confession-repentance” are an explicit verbal statement to G-d that a) you have sinned with a detailed specification of the sin b) you regret and are embarrassed by the sin and c) you resolve never to do this act again.
Again: Since Christianity does not believe in man’s capacity to remove failure and sin, a Christian may find the above ideas alien. But personal-repentance/forgiveness is the Jewish response to sin. (In particular Judaism does not require external acts such as the death of a righteous person to atone for sins–all repentance is based on PERSONAL endeavor. This, emphasis on personal responsibility, is an important point in Judaism).
Maimonides summarizes all the above concepts in the first chapter of his Laws of Repentance. You may find English translations of the Rambam in the library and on the internet. You can then read the first chapter of the “Laws of Repentance.” One such public source is http://www.milechai.com/product/mishnehT.html, where you can purchase the Laws of Repentance “Hichot Teshuvah” online. You don’t need a technical background to read this work.
I now summarize excerpts from Chapter 1. “Suppose you sin and repent. What are the consequences” We enumerate 4 cases:
Case 1: You violated a positive commandment which does not have a Biblical penalty of cut-off Then the repentance effectuates IMMEDIATE forgiveness.
Case 2: You violated a negative prohibition which does not have a Biblical penalty of execution or cut-off: Then repentance delays punishment until the next Yom Kippur, and the day of Yom Kippur itself atones.
Case 3: You violated a negative prohibition which has a capital punishment (or a punishment of cut off). Then repentance and the day of atonement delay punishment. The atonement (starting over again clean) is effected by sickness, poverty or other punishments.
Case 4: You desecrated God’s name in public. Then repentance, the Day of atonement (Yom Kippur) and sufferings delay the punishment and the day of death achieves complete atonement.
The above is a very brief summary of a vast and rich topic. Many medieval books were written which beautifully describe HOW to avoid repeating a sin. I urge you to purchase the books I mention above and do your own internet research to find similar books.