The Great Wall of China wasn't just designed to keep the barbarians out. It was also designed to keep the non-barbarians in. Here's why.
For much of human history across Asia and Africa, political rulers have faced a problem that is nearly unimaginable in today's densely-populated world. The basic problem was that their subjects kept running away. When our primitive ancestors first settled down to form static farming communities, productive land was much more abundant than people to farm it. Land where one could make a living through means other than farming was even more abundant. The rulers of nascent states who wanted to support luxuries for themselves and their courts - along with armies and priesthoods - as well as building public facilities like roads and irrigation systems for rice had to extract the surplus production from a population of farmers.
This meant that the most elementary task for a pre-modern ruler in many parts of the world was the acquisition and retention of manpower. We hence find that the power of such individuals was often defined not by how much land they controlled, but how many people were answerable to them. Slavery flourished in many parts of the world for much of human history, as those captured in wars were forced to return to the homeland of the capturing state to provide manpower. The population of slaves in many Thai, Burmese and Indochinese states was equal to or exceeded the number of normal citizens. Normal citizens, meanwhile, would often choose to run away from the centres of state power if demands for taxation, forced labour or military recruitment became too onerous.
This brings us back to the Great Wall of China. For most of the time that the various walls in northern China were being constructed or actively maintained - basically before the Qing dynasty took power in 1644 - one of their main purposes wasn't just to allow for military defence from northern barbarians, but also to control the migration of Chinese to the vast open spaces to the north where they could start a new life on plentiful land, outside of the demands of rapacious elites south of the Great Wall. Imagine the pre-modern world as a blank canvass with a series of ink blots strewn around; where the ink spreads is state power, and the blank canvass between are areas free of the demands that states place on people they control. Epidemics, wars, rebellions, or high taxes could all be enough to encourage people to flee the ink which, unlike today, covered nothing like all of the canvass.
The Ming dynasty, which ruled China before the Qing, conducted frequent censuses of the population and taxable resources of the areas it controlled, staking a claim to the wealth and labour of the hundreds of millions of Chinese people in its territory. The lure of the ungoverned northern frontier - and of the southwestern frontier, which Chinese rulers never tried to control so systematically - was obvious. Thence lay productive land outside of the state's control. Previous dynasties had provided less stability, but the periodic disorders, rebellions and civil wars that wracked Chinese territory prior to the Ming were another reason to flee.
Far from there being a deep, primordial difference between the Chinese and the "barbarians", viewed over centuries there was a long process by which groups moved across the frontier in either direction. Traditional Chinese texts on statecraft and government are shot through with adages about how to keep people within the remit of the state. The wall was one attempt to do so. The phrase "Han-traitor" was used to describe people who left the Han Chinese political system to go to live beyond its limits, where they immediately came to be seen as barbarians themselves. The stridency of official rhetoric about "barbarians" only gives us further notice of how serious a problem Chinese rulers saw this as been.
Frontiers - such as the frontier in the United States - have always been a place inhabited by outlaws, outsiders and marginal groups. China's northern frontier was no different. Refugees from political disputes, minority ethnic and religious groups, those evading taxes or debts, and even masses of ordinary citizens fleeing the demands of the state - all of these people could be tempted to flee to the areas where the Chinese state could not reach them. Such population movements on a large scale had been the death of not a few ancient states in Asia; indeed, much of south-east Asia was populated by groups that had fled the expansion of Chinese power in the south. The wall was an attempt to hem people in to the north. Lacking modern tools such as the telegraph and the railroad that allowed the American government to extend its control over a continent, ancient Chinese rulers feared the dispersal of their population beyond where they could serve the state.
Seen in this way, the Great Wall of China wasn't just a way to militarily defend against the north, it was also a way to defend against the social, economic and political lure of the northern frontier - to establish a sharp distinction between "us" and the barbarians which would discourage any Han Chinese thinking of going over to the land of the barbarians. It was a project not only of self-defence but also of state control; and only when one dynasty came to rule over all of the territory we now call China, and when China became so densely populated that there were fewer opportunities for anyone to run away from the state and start their own new community - only then did the Great Wall fall into disrepair.
For more detail on this theme, although not much discussion of the Chinese example, see James C. Scott, The Art of Not Being Governed: An Anarchist History of Upland Southeast Asia (New Haven, CT: Yale University Press, 2009).