كتاب الإمارة
33  
The Book on Government
(13)
Chapter: The obligation of staying with the Jama'ah (main body) of the muslims when Fitn (tribulations) appear, and in all circumstances. The prohibition of refusing to obey and on splitting away from the Jama'ah
(13)
باب الأَمْرِ بِلُزُومِ الْجَمَاعَةِ عِنْدَ ظُهُورِ الْفِتَنِ وَتَحْذِيرِ الدُّعَاةِ إِلَى الْكُفْرِ ‏‏
Sahih Muslim 1848 c

It has been narrated (through a different chain of transmitters) on the authority of Abu Huraira that the Messenger of Allah (ﷺ) said:

Whoever defects from obedience (to the Amir) and separates from the main body of the Muslim - and dies in that state - dies the death of one belonging to the days of jahiliyya. And he who is killed under the banner of a man who is blind (to the cause for which he is fighting), who gets flared up with family pride and fights for his tribe is not from my Ummah, and whosoever from my followers attacks my followers (indiscriminately) killing the righteous and the wicked of them, sparing not (even) those staunch in faith and fulfilling not his obligation towards them who have been given a pledge (of security), is not from me (i.e. is not my follower).
وَحَدَّثَنِي زُهَيْرُ بْنُ حَرْبٍ، حَدَّثَنَا عَبْدُ الرَّحْمَنِ بْنُ مَهْدِيٍّ، حَدَّثَنَا مَهْدِيُّ بْنُ مَيْمُونٍ، عَنْ غَيْلاَنَ بْنِ جَرِيرٍ، عَنْ زِيَادِ بْنِ رِيَاحٍ، عَنْ أَبِي هُرَيْرَةَ، قَالَ قَالَ رَسُولُ اللَّهِ صلى الله عليه وسلم ‏ "‏ مَنْ خَرَجَ مِنَ الطَّاعَةِ وَفَارَقَ الْجَمَاعَةَ ثُمَّ مَاتَ مَاتَ مِيتَةً جَاهِلِيَّةً وَمَنْ قُتِلَ تَحْتَ رَايَةٍ عُمِّيَّةٍ يَغْضَبُ لِلْعَصَبَةِ وَيُقَاتِلُ لِلْعَصَبَةِ فَلَيْسَ مِنْ أُمَّتِي وَمَنْ خَرَجَ مِنْ أُمَّتِي عَلَى أُمَّتِي يَضْرِبُ بَرَّهَا وَفَاجِرَهَا لاَ يَتَحَاشَ مِنْ مُؤْمِنِهَا وَلاَ يَفِي بِذِي عَهْدِهَا فَلَيْسَ مِنِّي ‏"‏ ‏.‏
Reference : Sahih Muslim 1848c
In-book reference : Book 33, Hadith 85
USC-MSA web (English) reference : Book 20, Hadith 4557
  (deprecated numbering scheme)