Many cities were republics up to the 1300s, when there was only 3. The rest were basically ruled by a signore. Some of those would manage to receive a title of nobility, sometimes even buying them from the king. Those rulers would remain inside the cities to rule them.