“Ahimsa (non-violence) is the ultimate Dharma/Duty” says Krishna in Bhagwadgita, at the same time convincing Arjun to fight a war. It seems contradictory to many. The main reason for the contradiction comes from the definition of Ahimsa taught by Mahatma Gandhi, wherein if someone hits to your one cheek, offer your second cheek as response. But this is not how Hinduism looks at Ahimsa.
One of the easiest way to understand Ahimsa is looking at how Ram approached to war with Ravan. Even though he had every valid reason to attack Ravana, he choose to follow diplomacy until the last moment, sending Angad for final dialog to avoid war. When nothing worked he approached war, and as per Hinduism this kind of war is dharma, and considered Ahimsa.
Another example to understand is Krishna moving Mathura to Dwarka, in order to avoid constant confrontation with Jarasandh. It was not that Krishna did not have a means to fight with Jarasandh every time he attacks, but that was not worth it, as he found a way to avoid the unnecessary war. Even though people called him bad words for this action, he still did it. This is far we need to go, in order to avoid unnecessary violence. Even after going this far, if the war becomes necessary, that war is considered Ahimsa.
In short, Ahimsa in Hinduism does not mean non-voilence in Gandhian way, it’s more like avoiding violence in every possible way, but when nothing works, then resorting to violence for greater good is still Ahimsa. For ex: Soldier defending his/her country is still doing Ahimsa, even though they have to kill enemy soldiers; or defending yourself after being attacked in self-defense is also Ahimsa.
BTW, while we don’t face war situations in our daily life in literal term, but we go through situations where we can harm someone physically, through words or by our actions. In those situation, we need to follow the above examples to solve the situation, and if we do that wholeheartedly we would be living life of Ahimsa.