Just war theory
Origins: 

Ancient Egypt: A 2017 study found that the just war tradition can be traced as far back as to Ancient Egypt. Egyptian ethics of war usually centered on three main ideas, these including the cosmological role of Egypt, the pharaoh as a divine office and executor of the will of the gods, and the superiority of the Egyptian state and population over all other states and peoples. Egyptian political theology held that the pharaoh had the exclusive legitimacy in justly initiating a war, usually claimed to carry out the will of the gods. Senusret I, in the Twelfth Dynasty, claimed, "I was nursed to be a conqueror...his [Atum's] son and his protector, he gave me to conquer what he conquered." Later pharaohs also considered their sonship of the god Amun-Re as granting them absolute ability to declare war on the deity's behalf. Pharaohs often visited temples prior to initiating campaigns, where the pharaoh was believed to receive their commands of war from the deities. For example, Kamose claimed that "I went north because I was strong (enough) to attack the Asiatics through the command of Amon, the just of counsels." A stele erected by Thutmose III at the Temple of Amun at Karnak "provides an unequivocal statement of the pharaoh's divine mandate to wage war on his enemies." As the period of the New Kingdom progressed and Egypt heightened its territorial ambition, so did the invocation of just war aid the justification of these efforts. The universal principle of Maat, signifying order and justice, was central to the Egyptian notion of just war and its ability to guarantee Egypt virtually no limits on what it could take, do, or use to guarantee the ambitions of the state.

India: The Indian Hindu epic, the Mahabharata, offers the first written discussions of a "just war" (dharma-yuddha or "righteous war"). In it, one of five ruling brothers (Pandavas) asks if the suffering caused by war can ever be justified. A long discussion then ensues between the siblings, establishing criteria like proportionality (chariots cannot attack cavalry, only other chariots; no attacking people in distress), just means (no poisoned or barbed arrows), just cause (no attacking out of rage), and fair treatment of captives and the wounded.
In Sikhism, the term dharamyudh describes a war that is fought for just, righteous or religious reasons, especially in defence of one's own beliefs. Though some core tenets in the Sikh religion are understood to emphasise peace and nonviolence, especially before the 1606 execution of Guru Arjan by Mughal Emperor Jahangir, military force may be justified if all peaceful means to settle a conflict have been exhausted, thus resulting in a dharamyudh.

East Asian: Chinese philosophy produced a massive body of work on warfare, much of it during the Zhou dynasty, especially the Warring States era. War was justified only as a last resort and only by the rightful sovereign; however, questioning the decision of the emperor concerning the necessity of a military action was not permissible. The success of a military campaign was sufficient proof that the campaign had been righteous.
Japan did not develop its own doctrine of just war but between the 5th and the 7th centuries drew heavily from Chinese philosophy, and especially Confucian views. As part of the Japanese campaign to take the northeastern island Honshu, Japanese military action was portrayed as an effort to "pacify" the Emishi people, who were likened to "bandits" and "wild-hearted wolf cubs" and accused of invading Japan's frontier lands.

Ancient Greece and Rome: The notion of just war in Europe originates and is developed first in ancient Greece and then in the Roman Empire.
It was Aristotle who first introduced the concept and terminology to the Hellenic world that called war a last resort requiring conduct that would allow the restoration of peace. Aristotle argues that the cultivation of a military is necessary and good for the purpose of self-defense, not for conquering: "The proper object of practising military training is not in order that men may enslave those who do not deserve slavery, but in order that first they may themselves avoid becoming enslaved to others" (Politics, Book 7).
In ancient Rome, a "just cause" for war might include the necessity of repelling an invasion, or retaliation for pillaging or a breach of treaty. War was always potentially nefas ("wrong, forbidden"), and risked religious pollution and divine disfavor. A "just war" (bellum iustum) thus required a ritualized declaration by the fetial priests. More broadly, conventions of war and treaty-making were part of the ius gentium, the "law of nations", the customary moral obligations regarded as innate and universal to human beings.

Christian views: Christian theory of the Just War begins around the time of Augustine of Hippo The Just War theory, with some amendments, is still used by 
Christians today as a guide to whether or not a war can be justified.
War may be necessary and right, even though it may not be good. In the case of a country that has been invaded by an occupying force, war may be the only way to restore justice.

Saint Augustine: Saint Augustine held that individuals should not resort immediately to violence, but God has given the sword to government for a good reason (based upon Romans 13:4). In Contra Faustum Manichaeum book 22 sections 69–76, Augustine argues that Christians, as part of a government, need not be ashamed of protecting peace and punishing wickedness when they are forced to do so by a government. Augustine asserted that was a personal and philosophical stance: "What is here required is not a bodily action, but an inward disposition. The sacred seat of virtue is the heart."

Nonetheless, he asserted, peacefulness in the face of a grave wrong that could be stopped by only violence would be a sin. Defense of one's self or others could be a necessity, especially when it is authorized by a legitimate authority:They who have waged war in obedience to the divine command, or in conformity with His laws, have represented in their persons the public justice or the wisdom of government, and in this capacity have put to death wicked men; such persons have by no means violated the commandment, "Thou shalt not kill."While not breaking down the conditions necessary for war to be just, Augustine nonetheless originated the very phrase itself in his work The City of God:
But, say they, the wise man will wage Just Wars. As if he would not all the rather lament the necessity of just wars, if he remembers that he is a man; for if they were not just he would not wage them, and would therefore be delivered from all wars.
Augustine further taught:

No war is undertaken by a good state except on behalf of good faith or for safety.
J. Mark Mattox writes,In terms of the traditional notion of jus ad bellum (justice of war, that is, the circumstances in which wars can be justly fought), war is a coping mechanism for righteous sovereigns who would ensure that their violent international encounters are minimal, a reflection of the Divine Will to the greatest extent possible, and always justified.  In terms of the traditional notion of jus in bello (justice in war, or the moral considerations which ought to constrain the use of violence in war), war is a coping mechanism for righteous combatants who, by divine edict, have no choice but to subject themselves to their political masters and seek to ensure that they execute their war-fighting duty as justly as possible.

Isidore of Seville: Isidore of Seville writes:

Those wars are unjust which are undertaken without cause. For aside from vengeance or to fight off enemies no just war can be waged.

Peace and Truce of God: The medieval Peace of God (Latin: pax dei) was a 10th century mass movement in Western Europe instigated by the clergy that granted immunity from violence for non-combatants. 
Starting in the 11th Century, the Truce of God (Latin: treuga dei) involved Church rules that successfully limited when and where fighting could occur: Catholic forces (e.g. of warring barons) could not fight each other on Sundays, Thursdays, holidays, the entirety of Lent and Advent and other times, severely disrupting the conduct of wars. The 1179 Third Council of the Lateran adopted a version of it for the whole church.

Saint Thomas Aquinas: The just war theory by Thomas Aquinas has had a lasting impact on later generations of thinkers and was part of an emerging consensus in Medieval Europe on just war. In the 13th century Aquinas reflected in detail on peace and war. Aquinas was a Dominican friar and contemplated the teachings of the Bible on peace and war in combination with ideas from Aristotle, Plato, Socrates, Saint Augustine and other philosophers whose writings are part of the Western canon. Aquinas' views on war drew heavily on the Decretum Gratiani, a book the Italian monk Gratian had compiled with passages from the Bible. After its publication in the 12th century, the Decretum Gratiani had been republished with commentary from Pope Innocent IV and the Dominican friar Raymond of Penafort. Other significant influences on Aquinas just war theory were Alexander of Hales and Henry of Segusio.
In Summa Theologica Aquinas asserted that it is not always a sin to wage war, and he set out criteria for a just war.