At the time of Hajj (done in an invalid way by the blasphemers at the time), the Prophet, peace and blessings be upon him, used to meet with the various tribes and call them to Islam.
During one of these times, he met with a group from Yathrib (Madinah). He called them to Islam, and they believed. They said to him: ”We will return to Yathrib and convey Islam to our relatives there.” They also told him that they would meet with him the following year. When they returned to Madinah, they called the people to Islam, and some of the people there became Muslim. These were called the Ansar (Supporters). In the following year, twelve men from the Ansar came and gave allegiance to the Prophet ﷺ. They said that they would not associate partners with God, steal, fornicate, kill their children (this was a practice during the time of ignorance, before the Prophet’s call to Islam), or disobey him. The Prophet, peace and blessings be upon him, sent Mus^ab ibn ^Umayr to teach them religious matters. Many of the residents of Madinah became Muslim, until every single family had at least one Muslim in it.
During the next season of Hajj, around seventy of the Ansar came to Makkah and gave allegiance to the Prophet ﷺ during the night.
Then, God revealed to our Prophet that he and his followers must immigrate to Madinah. The blasphemers of Quraysh found out about the allegiance and that the Prophet had ordered his Companions to go to Madinah. They tried to prevent Muslims from leaving to Madinah. So, many Muslims left secretly. The blasphemers had a meeting and decided to kill Prophet Muhammad ﷺ also before he left.
Gabriel came to the Prophet, peace and blessings be upon him and told him of the blasphemers’ plan. He also told him not to sleep where he usually did. The Prophet ﷺ then ordered his twenty-three-year-old cousin ^Aliyy to sleep in his place and to cover himself with his covers. He also instructed him to return to their owners what the Prophet ﷺ was safekeeping for people. When the Prophet went out of his house, there were several young men surrounding it ready to kill him. God made the men unable to see our Prophet. Our Prophet ﷺ took a handful of dirt and sprinkled it on their heads, while reciting the first nine verses of Suratu-Yasin. In these verses, God tells that He made the blasphemers unable to see Prophet Muhammad ﷺ .
Suratu-Yasin, Verses 1-12