To be precise, a distinction on paper was made between imperial cities (Reichsstädte) and free cities (freie Städte). Free cities were each formerly governed by a prince-bishop but had gained independence from their bishop during the High Middle Ages. They were Basel (1000), Worms (1074), Mainz (1244, revoked 1462), Ratisbon (1245), Strasbourg (1272), Cologne (1288) and Speyer (1294). Although the legal details varied greatly among them, Free Cities originally had more rights and privileges than Imperial Cities. For example, they only had to support the Emperor during the crusades and organise their own protection, while Imperial Cities also had to pay taxes to the Emperor and supply troops for his military campaigns.
But over time, the difference became more and more blurred so that the "Free and Imperial Cities" were collectively known in the Diet as "Free Imperial Cities". Rather than legal matters, what mattered more was the difference in wealth: rich cities, such as Lübeck or Augsburg, were genuinely self-ruling enclaves within the Empire; they waged war and made peace, controlled their own trade and permitted little interference from outside. In the later Middle Ages, many free cities formed alliances (Städtebünde); most notably the Hanseatic League, although some of their members were never Free cities and joined with the permission of their territorial ruler.
The cities gained (and sometimes lost) their freedom among the vicissitudes of medieval power politics. Some favored cities gained a charter by gift and others were wealthy enough to purchase theirs from a prince in need of cash; some won it by force of arms, others took it during times of chaos; a number of cities became free through the extinction of dominant families, like the Hohenstaufen.
Some free cities lost their privileges. Some voluntarily placed themselves under the protection of a territorial magnate. Others, like Donauwörth in 1607, were stripped of their privileges by the emperor on genuine or trumped-up offenses; others were pawned away by the Emperor, such as Mühlhausen, Duisburg and Offenburg, although the latter was able to regain its immediacy.
Free and imperial cities were only officially admitted as a Reichsstand to the Reichstag in 1489, and even then their votes were usually considered only advisory compared to the Benches of the Kurfürsten (Electors) and the Princes. The leagues of cities divided themselves into two groups, or benches, in the Imperial Diet, the Rhenish and the Swabian. By the time of the Peace of Westphalia (1648), the cities constituted a formal third "college" in the Diet. The most powerful Reichsstädte included Augsburg, Bremen, Cologne, Frankfurt, Hamburg, Lübeck and Nuremberg.
The number of imperial free cities varied greatly over the centuries, as did their geographic distribution. In general, there were many more free cities in areas with a diverse and scattered political structure, than in areas where larger territories had established themselves. The 1911 Encyclopædia Britannica mentions a list drawn up in 1422 with 75 free cities, and another drawn up in 1521 with 84. Territorial consolidation gradually shrunk the number to the 51 cities present at the 1792 Reichstag towards the end of the Empire. Many of those were in the Southwest and Franconia, some in the North and West, none in the East; most were former members of the formerly powerful Hanseatic League.
In the 16th and 17th century, a number of free cities were separated from the Empire due to external territorial change. The troops of Henry II of France seized the free cities connected to the Three Bishoprics of Metz, Verdun and Toul. Similarly, the maréchals of Louis XIV of France seized many cities based on claims produced by his Chambers of Reunion. That way, in Alsace, Strasbourg and the ten cities of the Décapole were annexed. Also, when the Swiss Confederacy gained its independence from the Empire in 1648, the Swiss imperial cities such as Basel, Berne and Zürich left the Empire as cantons of the confederacy.
With the rise of Revolutionary France in Europe, this trend would accelerate enormously. First between 1789 and 1792 the areas west of the Rhine were annexed by the revolutionary armies ending the long tradition of free cities as diverse as Cologne, Aachen, Düren, Speyer and Worms. Then, the Napoleonic Wars led to the reorganization of the Empire in 1803 (see German Mediatisation), where all of the free cities but six — the Hanseatic cities of Hamburg, Bremen, and Lübeck, and the cities of Frankfurt, Augsburg, and Nuremberg — were eliminated. Finally, Napoléon dissolved the Empire in 1806. By 1811, all of the free cities had been eliminated — Augsburg and Nuremberg had been annexed by Bavaria, Frankfurt had become the center of the Grand Duchy of Frankfurt, a Napoleonic puppet state, and the three Hanseatic cities had been directly annexed by France as part of its effort to enforce the Continental Blockade against Britain. Hamburg and Lübeck with surrounding territories formed the département Bouches-de-l'Elbe, and Bremen the Bouches-du-Weser.
When the German Confederation was established in 1815, Hamburg, Lübeck, Bremen and Frankfurt were once again made free cities. Frankfurt was annexed by Prussia in consequence of the part it took in the Austro-Prussian War of 1866. The three Hanseatic cities remained as constituent states of the new German Empire, and retained this role in the Weimar Republic and into the Third Reich, although under Hitler this status was purely notional. Due to Hitler's distaste for Lübeck and the need to compensate Prussia for its territorial losses under the Greater Hamburg Law, it was annexed to the latter in 1937. In the Federal Republic of Germany which was established after the war, Bremen and Hamburg became constituent states (Länder), a status which they retain to the present day. Berlin, which had never been a free city in its history, also received the status of a state after the war due to its status in divided post-war Germany.