How committing sins keeps us away from Imam Mahdi?

To answer the question,the following notes would be beneficial:

1- The Imam is the agent of Allah and acts exactly as Allah wants and desires so, if our sins are so hideous that they mean we are opposing Allah it, of course, would mean that we have moved against the Imam.

2- Committing sins makes one distances from his Imam and it widens the gap between man and the Imam. A real Moslem should protect his faith and should refrain even from the minor sins. It is narrated from Imam Sādiq to have said,  “One who is submissive to Allah can claim to love us and those who disobey Allah, they are our enemies. No one can claim to accept our guardianship but through piety and good actions.”

(Usūl al-kāfī vol. 2, p. 74, hadith 3.)

3- There is a criterion to realize how much one loves God and the Imam or opposes them. Imam Ali bin Abī Tālib says,

 “If one wants to know how his statues is with God he should pay attention how much God is great to him when he faces an (alluring) sin. His position with God is equal to the position of God to him.” (Bihār al-Anwār, vol. 67, p. 18, hadith 11.)

Exactly the same, if one wants to know how much the Imam will honor him, he should look how much he would honor the Imam (who sees our actions) when he faces a sin. It means that when man commits a sin he has dishonored God and the Imam and dismissed their presence.

4 – If one commits a sin and does not repent it will becomes easier for him to do the next sin. Man should be cautious that it would result in such a degradation that would be unimaginable at first. This is what God says in the Quran,

 “Perdition was the end of those who committed the worst things: They denied the signs and miracles of Allah and made ridicule of them.” (The Quran, 30:10)

Such a man will be so drown in darkness that he would deny the Imam or would oppose him and his mission.