How to Stop Yourself From Sinning
Question

What is meant by sins committed when one is alone? And how can a person avoid them? I have a question about the hadith “sin is that which wavers in your heart and you do not want the people to find out about it.” 
What may be understood from the hadith is that people inherently feel ashamed of sin and try to conceal it from people. 
What is the connection between a sin being merely a sin that a person commits as he is not infallible, and this sin being regarded as one of the sins committed by a person on his own?
Praise be to Allah.If you sin, hasten to repent
The Muslim in this world is vulnerable to falling into sin and disobedience. What is required of him – if he does fall into sin – is to hasten to repent and seek forgiveness . Allah, may He be exalted, says (interpretation of the meaning):
“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.’” [az-Zumar 39:53]
“And whoever does a wrong or wrongs himself but then seeks forgiveness of Allah will find Allah Forgiving and Merciful.” [an-Nisa 4:110]
“And those who do not invoke with Allah another deity or kill the soul which Allah has forbidden [to be killed], except by right, and do not commit unlawful sexual intercourse. And whoever should do that will meet a penalty. Multiplied for him is the punishment on the Day of Resurrection, and he will abide therein humiliated –Except for those who repent, believe and do righteous work. For them Allah will replace their evil deeds with good. And ever is Allah Forgiving and Merciful. And he who repents and does righteousness does indeed turn to Allah with [accepted] repentance.” [al-Furqan 25:68-71]
Does Allah love those who repent?
Allah, may He be exalted, rejoices at the repentance of His slave when he repents to Him, as is mentioned in the hadith that was narrated from Anas ibn Malik (may Allah be pleased with him), who said: The Messenger of Allah (blessings and peace of Allah be upon him) said: Allah rejoices more over the repentance of His slave when he repents to Him than one of you who was on his mount in the wilderness, then he lost it, and his food and drink are on it, and he despairs of finding it. He goes to a tree and lies down in its shade, having lost hope of finding his mount, and whilst he is like that, there it is standing in front of him, so he takes hold of its reins and says, because of his intense joy, ‘O Allah, You are my slave and I am your lord,’ making this mistake because of his intense joy.”. (Narrated by al-Bukhari (5950) and Muslim (2747)
Transgressing the limits of Allah when alone 
What is required of the Muslim is to beware of falling into sins committed when he is on his own, for Allah, may He be exalted, criticized the one who conceals his sin from people, but does not try to conceal it from Allah, as He, may He be exalted, says (interpretation of the meaning):
“They may hide (their crimes) from men, but they cannot hide (them) from Allâh; for He is with them (by His Knowledge), when they plot by night in words that He does not approve. And Allâh ever encompasses what they do.” [an-Nisa 4:108]
The Prophet (blessings and peace of Allah be upon him) warned against sins committed when one is alone and in secret, as is mentioned in the hadith narrated from Thawban (may Allah be pleased with him) from the Prophet (blessings and peace of Allah be upon him), that he said: “I certainly know people of my ummah who will come on the Day of Resurrection with good deeds like the mountains of Tihamah, but Allah will make them like scattered dust.” Thawban said: O Messenger of Allah, describe them to us and tell us more, so that we will not become of them unknowingly. He said: “They are your brothers and from your race, worshipping at night as you do, but they will be people who, when they are alone, transgress the sacred limits of Allah .” (Narrated by Ibn Majah (4245); classed as sahih by al-Albani in Sahih Ibn Majah)
How to stop sinning
With regard to how a person may avoid committing sins when he is alone, that may be achieved by means of the following:

Turning to Allah, may He be exalted, in supplication and beseeching Him to avert sins from him. Allah, may He be exalted, says (interpretation of the meaning): “And when My servants ask you, [O Muhammad], concerning Me - indeed I am near. I respond to the invocation of the supplicant when he calls upon Me. So let them respond to Me [by obedience] and believe in Me that they may be [rightly] guided.” [al-Baqarah 2:186]
Striving to control the nafs (self, and its evil inclinations), warding off its intrusive thoughts and trying to purify it by obeying Allah. Allah, may He be exalted, says (interpretation of the meaning): “And [by] the soul and He who proportioned it, and inspired it [with discernment of] its wickedness and its righteousness, he has succeeded who purifies it, and he has failed who instills it [with corruption]” [ash-Shams 91:7-10] and “And those who strive for Us - We will surely guide them to Our ways. And indeed, Allah is with the doers of good.” [al-'Ankabut 29:69]
Reflecting upon the stern warning mentioned in the hadith of Thawban quoted above, and fearing lest it apply to the one who commits those sins when he is alone.
Bearing in mind that Allah, may He be exalted, is always watching and sees the Muslim and whatever situation he is in.

Ibn Kathir (may Allah have mercy on him) said:
“It was narrated from Imam Ahmad (may Allah have mercy on him) that he used to recite these two lines of poetry, either to himself or to someone else:
If you are ever alone, do not say I am alone; rather say I have One Who is watching me.
Do not think that Allah is ever unaware even for a moment, or that anything is hidden from Him.” (Tafsir Ibn Kathir (6/219)

The Muslim should imagine that those whom he admires and respects can see him when he is committing that sin !

He should feel more shy before Allah than before people. According to a hadith narrated from the Prophet (blessings and peace of Allah be upon him): “Feel shy before Allah just as you would feel shy before a man of your family.” Classed as sahih by al-Albani (may Allah have mercy on him) in as-Silsilah as-Sahihah (3559); he attributed it to all-Bazzar and al-Mirwazi in al-Iman.

Remembering death and imagining if death were to come to him whilst he was committing that sin; how would he meet his Lord in that case?!
Remembering what Allah has prepared for His righteous slaves of Paradise as vast as the heavens and the earth, and thinking of the punishment of Allah, may He be exalted. Allah, may He be exalted, says (interpretation of the meaning): “So, is he who is cast into the Fire better or he who comes secure on the Day of Resurrection?.” [Fussilat 41:40] 

How do you identify a sin?
With regard to the hadith of an-Nawwas ibn Sam‘an al-Ansari (may Allah be pleased with him), who said: I asked the Messenger of Allah (blessings and peace of Allah be upon him) about righteousness and sin and he said: “Righteousness is a good attitude, and sin is that which wavers in your heart and you do not want the people to find out about it” - This is the criterion for determining what is sin , when there is any uncertainty about the matter.
An-Nawawi (may Allah have mercy on him) said, commenting on this hadith:
“What is meant by “wavers in your heart” is that there is uncertainty and hesitation, one does not feel comfortable or at ease with it, and there is doubt and fear that it may be a sin.” (Sharh Muslim (16/111)
Ibn Rajab (may Allah have mercy on him) said:
“This indicates that sin is that which causes discomfort, anxiety, worry, and unease, and one does not feel comfortable or at ease with it; at the same time, it is something that people find abhorrent and they criticize it if they find out about it. This is the clearest indication of sin when there is any uncertainty about it.” (Jami‘ al-‘Ulum wa’l-Hikam (p. 254)
Shaykh Muhammad ibn ‘Uthaymin (may Allah have mercy on him) said:
“This sentence, “sin is that which wavers in your heart”, only applies to one whose heart is pure and sound. He is the one in whose heart that which is sin wavers, and he does not want people to find out about it.
As for those who are rebellious and drift away from the path of obedience, whose hearts have grown hard, they do not care; in fact they may brag about committing evil and sin. So the words here are not applicable to everyone; rather they apply specifically to those whose hearts are sound and pure. If they commit sin, even if they did not know beforehand of the religious text that prohibits it, you will find them wavering and not wanting people to find out about it. This is a guideline, not a principle; in other words, the indication that it is a sin may be in a believer’s heart.” (Sharh al-Arba‘in an-Nawawiyyah (p. 294)
Based on that, there is nothing in the hadith to indicate any approval of a person committing sins when he is alone. Rather this is a precise criterion in the Muslim’s heart regarding matters that he is uncertain as to whether something is a sin or not.
If he would feel ashamed of his action in front of other people, then it is a sin, and if he would not feel ashamed of it, then it is permissible and there is nothing wrong with it.
We ask Allah to protect us and you from sins and acts of disobedience, and to help us to restrain our lower selves (nafs and evil inclinations).
And Allah knows best.