Oh boy what a process they were!
The super short version (scroll down, it's still pretty long) is that for about 800 years, both were part of the Holy Roman Empire. So when Napoleon comes rampaging through in the early 1800s, he kicks the whole Empire to pieces, and all of a sudden, dozens of kingdoms that had been part of it (e.g., Lombardy-Venetia, Piedmont-Sardinia, Tuscany, etc., and Bavaria, Westphalia, Saxony, and for complicated reasons especially Schleswig-Holstein, etc.) are all cast adrift and trying to figure out what happens now, sort of like if someone came into your school and said "there is no such a thing as a principal starting now!"
A lot of these kingdoms get claimed by Austria since Francis II stayed Emperor of Austria when he gave up the title Holy Roman Emperor, but just like if the principal was also your math teacher, if they try to run the school without being recognized as principal, a lot of people are going to start wondering, "why should we listen to THIS bozo?" So a lot of these kingdoms start saying, "why are we listening to this Austrian guy? I have more in common with my neighbors over here than I do with Austrians!"
The upshot is there's a lot of political and economic jockeying around--in Germany, you'll want to look at how the Confederation of the Rhine forms and gets replaced by the German Confederation, and how the 1834 creation of the Zollverein changes economic relationships, and in Italy, you'll want to look at how revolutionary groups like the Carbonari start remembering the good old days of Ancient Rome and amping up their resentment at being kicked around so much, by the Austrians, then Napoleon, then the Austrians again.
There's a lot of plotting, a lot of betrayal, and a whole bunch of wars and mostly failed revolutions, but eventually power starts consolidating, little kingdoms start merging into bigger kingdoms, a few key battles get won, and they declare independence and defend it, but it's far from a simple process.
And nationalism. I've been hinting at nationalism throughout. It really takes off in the wake of the French Revolution, and becomes the emotional jet fuel of this whole bloody process. It's that change in perspective from, "I'm a subject of a king, and whatever he chooses to do with me is his decision," to "we have a unique culture and history that defines us, so don't even think about coming in from some other place and telling me what to do. I'm from Italy, the land of Caesar and Augustus and Trajan (or Germany, the land favored by Wotan, that birthed the heroes of the Teutoburg Forest who even the Romans couldn't conquer), and no inbred Hapsburg rules me!" A big reason that so much German art and lit comes from the early-mid 1800s is that writing explicitly nationalistic political tracts was illegal, so nationalism found its expression in art. Wagner's operas and the Grimm brothers' fairy tale collections, for instance, are really political statements, telling their audiences: "you're not just some Westphalian under Austria's thumb; you're a German!"