Abu Salama b. 'Abd al-Rahman reported that Jabir told him that he had observed the prayer in danger with the Messenger of Allah ﷺ. The Messenger of Allah ﷺ (first) led one of the two groups in two rak'ah of prayer. and then led the second group in two rak'ah of prayer. So the Messenger of Allah ﷺ observed four rak'ah and led in two rak'ah each of the groups.
أَخْبَرَهُ أَنَّهُ صَلَّى مَعَ رَسُولِ اللَّهِ ﷺ صَلاَةَ الْخَوْفِ فَصَلَّى رَسُولُ اللَّهِ ﷺ بِإِحْدَى الطَّائِفَتَيْنِ رَكْعَتَيْنِ ثُمَّ صَلَّى بِالطَّائِفَةِ الأُخْرَى رَكْعَتَيْنِ فَصَلَّى رَسُولُ اللَّهِ ﷺ أَرْبَعَ رَكَعَاتٍ وَصَلَّى بِكُلِّ طَائِفَةٍ رَكْعَتَيْنِ