The Great Wall did keep the Chinese in, as well as kept the invaders out.
But back in those days, long ago, the population wasn’t that huge and China was very fertile and self-sufficient.
The Qin Emperor had just united China and he was then more concerned about pacifying his people. The Chinese States were quite disjointed and internal security was his priority. The Great Wall helped to keep the Northern threat out so he could focus within.
Anyway, the Great Wall had big doors to allow troops out in case they feel like attacking the North one day (which subsequent dynasties did).
According to ancient Chinese myths, back in 3500 BC, the first civilised Chinese people were pioneers and spread themselves from Shandong province across the land, creating their own tribes.
After that, China became like most Empires - conquer and assimilate.
China was like Europe - different provinces were like different countries with their own language and culture.
To keep a 5,000 year old story short, after Qin Dynasty, many territories were lost and recovered. The borders expanded and grew until it relatively stabilized during the Qing Dynasty.
I think it’s quite interesting that formerly nomadic rulers of northern China, like the Khitan Liao dynasty and the Jurchen Jin dynasty alsobuilt walls on their northern borders once they settled in to protect against not so formerly nomadic people from the north.
I recall an article in Economist about Chinese linguistics. There are areas in China which long ago used to be separate ethnic groups. The empire eventually ovrran them and assimilated, imposed the main language… (like welsh or scots?) Today the only evidence is things like odd grammar constructs - the Han words overlaid on ethnic sentence structure. OTOH areas like Canton still have a different language, even if the graphical writing is thevsame.
This is also why the Chinese government is so unusually stubborn about admitting that some areas (Tibet? Taiwan?) are capable of or could be independent. The central empire expanded to take over the entire area. Then, during times of trouble or weak central government, warlords would declare their provinces independent of the centre. Accepting independence from any area that at anytime was a part of the empire would be admitting the government is weak. It’s a matter of pride.