There were many small free imperial cities and other tiny city-states in Medieval and Early Modern Germany, up to 300 by some counts. Why and how did only Hamburg and Bremen remain to this day as city-states in the new federal Germany?

by rustbeltliving
korhux

I am not sure about Hamburg, but in the case of Bremen the zoning of Germany among the Allies following WWII is an important aspect which also answers the question about Bremerhaven being part of Bremen.

Germany was divided into four zones - a Soviet zone (which eventually became the DDR), a British zone, a French zone and an American zone (those three eventually forming the BRD). The American zone has mainly been in south eastern and middle Germany, accessible only via air or the British zone coming from the North Sea. For the US to reach their zone, they demanded an own port with sea access - Bremen. Bremen itself, however, lies somewhat inland and has no direct access to the North Sea. That's why Bremerhaven - meaning Bremen's port - was along Bremen part of the American zone, although completely surrounded by the British zone. Via Bremen and Bremerhaven the US were able to supply their zone in the south. When the British, French an American zones formed the BRD in 1949, the structure of the Bundesländer was heavily influenced by those structures established by the Allies in each of their zones between 1945 and 1949 (which is also the reason why Germany has some Bundesländer without an 'own' cohesive history as a region e.g. Nordrhein-Westfalen, Rheinland-Pfalz or even Baden-Württemberg). Within the American zone Bremen was administered by itself due to its isolation from the rest of the American zone and remained a separate Bundesland even after 1949.

In short: Bremen today is a city state because unlike the surrounding area it was part of the American occupation zone after WWII. In the 1949 founded BRD it remained as its own Bundesland.

YeOldeOle

A small correction in regard to the question itself, not an answer:

There are now 3 city-states in Germany. Hamburg, Bremen and Berlin. Also, Bremen is a weird mix as it consists not only of Bremen proper but actually of a second city, some 70km away, Bremerhaven (Hamburg has some outlying parts as well, most notably probably Neuwerk, an island in the Wattenmeer, but is still considered a city state).

Temponautics

As a final note, one should not forget that the integration of most smaller city states into larger contiguous lands occurred in the HRE in several large pushes, most notably the large reforms after the thirty years' war (1648) and then again after the Congress of Vienna (1815, which is really the end of most of them, and of course happens after the demise of the HRE).
Hamburg and Bremen were among the few stronger survivors of these processes, which is why by the end of WWII the era of their independent city rule is still in living memory, while, say, a small city republic like Lindau at the Bodensee (Lake Konstanz) had already been subsumed by Napoleon into Bavaria by 1806, and had in Nazi times been romanticized as the "old" mythical medieval Germany, developing a significantly different flavor of a more "medieval" character, while someone from Hamburg would still proudly tell stories of their cities' independence until just recently. Hamburg and Bremen had existed post industrialization as city states - the other medieval city republics had not. Ergo in 1947/8, when modern Germany's founding fathers were writing up their new constitution looking towards a modern industrialized society, they were arguably not looking to reintroduce a reactionary image of a quaint medieval Germany that the Nazis had celebrated, especially given that the public had no appetite whatsoever for looking back.
Berlin is a special case: it had not been a city state of its own before, but since it was the capital of Prussia and the allies associated Prussia (justifiably) strongly with the age of German militarism 1871-1945, Prussia was formally declared dissolved (the only German state the allies insisted on having formally dissolve to exorcize it as such from history). Berlin, in other words, became a city state against the grain of its own history due to the aftermath of World War II, while Hamburg and Bremen became city states in line with their own histories.