So the Italian peninsula in Roman times was like unified country, how did it transition into the separate city states that were later unified again in the 19th century?
I think the simplest way would be to separate the answer of this question into preconditions and political developments.
The first thing to consider is the structure of the Roman Empire itself. My main Latin Prof in grad school, who specialized in the later empire, described the polity as a "commonwealth of cities." The nature of ancient governance is such that it made it virtually impossible for emperors to rule in anything but a reactive matter. In short, they relied on city councils to enact governance. The civitates of the empire were, in essence, "mini-Romes" with a similar set of temples and civic buildings. While local administration changed as governance, and especially judicial structures, were elided with church structures, the cities of the empire were largely self-governing to begin with.
Next, we should consider the places within Italy to begin with. Throughout the west, the empire established civitates and other settlements through which law and order was established. Now, no matter what people tell you about "celtic" or Iron Age cities, this sort of Roman Urban organization was not necessarily "natural" to the north. These cities were administrative centers. Thus, when the Western Empire collapsed, many of these cities went away. Yes, cities like Cologne (from the Roman colonia have Roman roots, but they served as administrative centers that weren't necessarily embedded in the political and social structures around them. Richard Hodges has made a career (Dark Age Economics or slightly more recently, his contributions to The Sixth Century: Population, Distribution, and Demand) demonstrate that the nucliei that would form the urban centers north of the Alps (the *emporia, et. al.) emerged from economic necessity.
In Italy, by contrast, urban society had much deeper roots. Indeed, we should not be surprised that the Roman administration via cities emerged from this context. Mediolanum (Milan) was first settled by the "Celtic" group, the Insubres, around 600, according to Livy. While this is certainly Livy looking back and trying to understand Lombardy in terms familiar to Rome, archaeology seems to bear urban forms emerging throughout the region (see just about any publication), but Livy, Strabo, and archaeological evidence seems to indicate the settlement of urban communities in northern Italy before the Roman conquest. The Biblioteca Storica di linguistica italiana produced a very comprehensive Toponymic dictionary in 1990, and it is illuminating to look at some of the older place in Italy.
So, the urban plan was better situated to survive the Roman fall. And as Germanic groups conquered and favored landed holdings, they maintained homes in the cities and political organization persisted there. This brings us to the political developments. As the local "Roman-Italian landed aristocracy melded with the Germanic groups they saw cities as important sites. As the Kingdom of Italy was bonded with Charlemagne's empire, the great man and his successors were increasingly concerned with with goings-on north of the Alps, locals started to take command. In the 10th century, one Berengar I of Istria, some sort of Frankish noble, came to rule over Italy as an appellate and subordinate king to Charles the Fat. His grandson, Berengar II, ruled with his son later that century, but their power was so weak, they had to grant away their privileges and authorities. They essentially granted away urban rights to collections of oligarchic elites of local and germanic extraction that were clustered around cities. Parsing out character of these urban elites has been of interest medieval Italian historiography, but the difficulty of identifying these groups is very difficult to do given the obscurity of the source material. We have, for example, the charter granted to Genoa, the earliest such documents. They address the group as Fideles, but this could mean any number of things. Basically, you have groups like this who would form the urban elites of the oligarchic republics of later in the Middle Ages.