People were here long before anyone built a town. In 1848 an adult Neanderthal skull came out of Forbes' Quarry on the north face, only the second Neanderthal fossil ever found. If anyone had understood it at the time, the species would be named after Gibraltar. The ancient Mediterranean treated the Rock as a landmark and a shrine, not a home: it had no easy fresh water, no good soil and no safe anchorage, so the Phoenicians founded Carteia near today's San Roque around 950 BC instead.
The name comes from the Moors. In 711 Tariq ibn Ziyad, governor of Tangier, landed here to begin the Muslim conquest of Iberia, and the Rock became Jabal Tariq, the Mountain of Tariq. The Moors were the first to settle it permanently, and an Almohad sultan fortified it in 1160. Castile took the town in 1309, lost it in 1333 and finally won it back in 1462 with a surprise attack.
Spanish rule ended in a few days in August 1704. An Anglo-Dutch fleet under Admiral Rooke, who had failed at other targets, bombarded the town and the governor agreed to surrender on the 4th. Almost all of the roughly 4,000 inhabitants walked out, and their refugee settlement grew into San Roque. Spain ceded Gibraltar to Britain in the 1713 Treaty of Utrecht.
Spain tried again and again. The worst was the Great Siege from 1779 to 1783, when French and Spanish fleets blockaded the Rock and a vast army dug in on land. The garrison answered with red-hot shot that set the floating batteries on fire. Bombardment aimed at civilians wiped out whatever Spanish-era architecture was left. The Rock's reputation for being impregnable gave English a phrase about being as strong as it, still current.
The people who live here now arrived in the 19th century. In 1813 Genoese and other Italians made up close to a third of residents, with Portuguese, Spaniards, Jews, British and Menorcans making up the rest. The epidemic of 1804 took more than a third of everyone on the Rock. Coaling needed labour, so Gibraltar hired Spanish workers, and the shanty town they built across the border became La Línea. By 1891 nearly 75% of residents had been born in Gibraltar.
In May 1940 Britain evacuated the whole civilian population, fearing Spain would join the war, and the soldiers who moved in dug over 50 kilometres of tunnels. The last evacuees did not get home until 1951. The evacuation strengthened a distinct Gibraltarian identity, not just a British one, and it is part of why people here are touchy about being called Spanish.
In September 1967 voters chose Britain over Spain by 12,138 to 44. Spain closed the frontier completely in 1969 and cut the phone lines, and families with relatives across it shouted to each other over the gates. Gibraltar replaced the missing Spanish workers with Moroccans. The border fully reopened in February 1985.
The military left and the economy had to change. The British garrison, in place since 1704, went home in 1990, and the government turned to tourism, financial services, duty-free shopping and online gambling. A tenth of today's land was reclaimed from the sea. As of 2011 the Rock drew over 10 million visits a year against 29,752 residents. In 2002 voters rejected shared sovereignty with Spain by about 17,000 to 187, and Spain still claims the territory, although the 2006 Cordoba Agreement eased the border.