The Khalsa itself was established in 1699 by the 10th Guru (Guru Gobind Singh Ji) of the Sikhs, but the idea of being ready to fight and carrying weapons while also being spiritual (so being a saint-soldier) has always been a part of Sikh identity, as the 1st Guru (Guru Nanak Dev Ji) carried a large stick on all his travels, and the 2nd Guru (Guru Angad Dev Ji) promoted wrestling/fighting sports.
In terms of fighting injustice, the 6th Guru (Guru Hargobind Sahib ji) was the first Guru who fought in wars during the early 1600s. He fought 4 battles winning all of them. This was the main push in militarizing the Sikh community, as all the Gurus after Guru Hargobind Sahib Ji carried weapons and were prepared for war or had previously fought in wars. The name of the 9th Guru (Guru Teg Bahadur Sahib Ji) who was the father of Guru Gobind Singh literally translates to "Strong with the Sword".
So overall, the community has always been able to defend itself because of the saint-soldier mindset being presented since the beginning.