Caution: Translations of Quran and Ḥadīth may lead to possible misapplications and misinterpretations. This site is intended for students of sacred knowledge that are proficient in comprehending classical Arabic and have a strong foundation in Islamic sciences. Also note that religious injunctions rely on several aspects beyond what one may glean through reading individual aḥādīth.
quran:9:40

If you do not aid the Prophet - Allah has already aided him when those who disbelieved had driven him out ˹of Makkah˺ as one of two, when they were in the cave and he said to his companion, "Do not grieve; indeed Allah is with us." And Allah sent down his tranquillity upon him and supported him with angels you did not see and made the word of those who disbelieved the lowest, while the word of Allah - that is the highest. And Allah is Exalted in Might and Wise.  

If you do not help him, that is, the Prophet (s), ˹know that˺ God has already helped him, when the disbelievers drove him forth, from Mecca, that is, they made him resort to leaving, when they desired to kill him or imprison him or banish him at the council assembly — the second of two (thāniya ithnayn: this is a circumstantial qualifier), that is, one of two, the other being Abū Bakr: in other words, just as God helped him in such a situation, He will not forsake him in another; when (idh substitutes for the previous idh) the two were in the cave — a breach in the mountain called Thawr — when (idh substituting again), he said to his companion, Abū Bakr — who, upon perceiving the ˹sound of the˺ feet of the idolaters ˹nearby˺, had said to him, ‘If one of them should merely look below his feet, he will definitely see us!’ — ‘Do not despair; verily God is with us’, assisting ˹us˺. Then God sent down His Spirit of Peace upon him, His reassurance — some say this means upon the Prophet, others, that it means upon Abū Bakr — and supported him, that is, the Prophet (s), with legions, of angels, you did not see, ˹both˺ in the cave and in the locations in which he fought battles; and He made the word of those who disbelieved, that is, the call to idolatry, the nethermost, the one vanquished, and the Word of God, that is, the profession of His Oneness (shahāda), was the uppermost, the one prevailing and triumphant. And God is Mighty, in His Kingdom, Wise, in His actions.
القرآن:٩:٤٠

إِلَّا تَنْصُرُوهُ فَقَدْ نَصَرَهُ اللَّهُ إِذْ أَخْرَجَهُ الَّذِينَ كَفَرُوا ثَانِيَ اثْنَيْنِ إِذْ هُمَا فِي الْغَارِ إِذْ يَقُولُ لِصَاحِبِهِ لَا تَحْزَنْ إِنَّ اللَّهَ مَعَنَا ۖ فَأَنْزَلَ اللَّهُ سَكِينَتَهُ عَلَيْهِ وَأَيَّدَهُ بِجُنُودٍ لَمْ تَرَوْهَا وَجَعَلَ كَلِمَةَ الَّذِينَ كَفَرُوا السُّفْلَىٰ ۗ وَكَلِمَةُ اللَّهِ هِيَ الْعُلْيَا ۗ وَاللَّهُ عَزِيزٌ حَكِيمٌ  

{إلاّ تنصروه} أي النبيَّ ﷺ {فقد نصره الله إذ} حين {أخرجه الذين كفروا} من مكة أي الجؤوه إلى الخروج لما أرادوا قتله أو حبسه أو نفيه بدار الندوة {ثاني اثنين} حال أي أحد اثنين والآخر أبو بكر - المعنى نصره الله في مثل تلك الحالة فلا يخذله في غيرها - {إذ} بدل من إذ قبله {هما في الغار} نقب في جبل ثور {إذ} بدل ثان {يقول لصاحبه} أبي بكر وقد قال له لما رأى أقدام المشركين لو نظر أحدهم تحت قدميه لأبصرنا {لا تحزن إن الله معنا} بنصره {فأنزل الله سكينته} طمأنينته {عليه} قيل على النبي * وقيل على أبي بكر {وأيَّده} أي النبي ﷺ {بجنود لم تروها} ملائكة في الغار ومواطن قتاله {وجعل كلمة الذين كفروا} أي دعوة الشرك {السفلى} المغلوبة {وكلمة الله} أي كلمة الشهادة {هي العليا} الظاهرة الغالبة {والله عزيز} في ملكه {حكيم} في صنعه.