When humans evolved, they did so in an environment of competing bands of people. As group evolutionary strategies evolved, altruism towards the group was beneficial, along with genocidal hatred for other competing groups and fanatical aggressiveness or bravery when it came to defending the tribe, what we call today patriotism and how we define heroes or martyrdom. The tribe, as a unified vehicle carrying more of the alleles for these traits competed with neighboring tribes, the more aggressive, genocidal, cohesive and intelligent tribe on average eliminated the lesser tribe (sometimes of course taking hostages). But slowly, humans that had bloodlust displaced the more peaceful tribes around them, and step-by-step humans became adapted for patriotism towards the group. Individualism was suppressed and cohesiveness became predominant. But all was not equal between different tribes.
The Environment of Evolutionary Adaptation
Our ancestors stayed put over thousands of years and developed under vary different environments. For example, xenophobia, group cohesion, tribal conflict, ethnocentrism was heightened in groups that evolved where people were in competition for resources and lived closer together. From Asia to the Mediterranean for example, more people could be supported by the natural resources available. There were many more people, more tribes, and more conflicts. Evolutionary pressures pushed competing groups towards higher frequencies of genetic alleles that favored aggressiveness against one's neighbor. Constant wars and conflicts accelerated this process, producing on average people who today would score much high on ethnocentrism and would have little time for helping or tolerating exploitation by the other. Weak and peaceful tribes were either killed or enslaved.
At the other extreme for example, northwestern Europeans evolved in an extremely harsh environment, one that was glaciated about 10,000 years ago, and supported very few people. Neighboring tribes were not close together, population density was low, and tribal conflict less salient than planning and making provisions for the long harsh winters. In fact, it would be easy to imagine that coming across unknown and relatively altruistic neighbors was live saving. These people evolved in an environment that nurtured compassion for the stranger, because often strangers were a needed resource and not a threat. The greatest threat came from the harsh winters, not from competing tribes. So altruism flourished over xenophobia and fear of the other. High frequencies of alleles for altruism, compassion, tolerance, and benevolence towards all were selected for. But within reason of course. As long as the other did not appear as a threat, reciprocal altruism was selected for over intolerance.