Until 1259, Milan was a free commune that elected its own podestà. The Torriani family gained sustained power in 1240, when Pagano Della Torre was elected podestà. After Pagano's death, Baldo Ghiringhelli was elected podestà in 1259, but at the end of his tenure Martino della Torre, Pagano's nephew, perpetrated a coup d'état, seizing of power of his family over the commune, establishing the first Signoria of Milan. During their tenure, the Torriani family, aligned with French Charles of Anjou, started a strong rivality with Visconti family, loyal to the German Hohenstaufen. In 1262, Pope Urban IV appointed Ottone Visconti as Archbishop of Milan, for Martino della Torre's disappointment. In 1273, a civil war started between the two families, ended with Torriani's defeat in the Battle of Desio of 1277. On June 1302, Guido della Torre forged a coalition with anti-Visconti cities and marched on Milan, deposing Visconti. However, in 1308 Guido started a quarrel with his cousin, the Archbishop Cassone della Torre. After an assault on Milan Cathedral, Cassone fled to Bologna and solicited an imperial intervention. Taking advantage of a chaotic situation in Northern Italy, King Henry VII of Germany descended into Italy with an army, and in Autumn 1310 he marched on Milan to restore both Cassone and the Visconti. After the fall of Milan, he was crowned King of Italy in the city's Cathedral.
In 1395, Gian Galeazzo Visconti was titled Duke of Milan by King Wenceslaus, who sold the title under the payment of circa 100,000 florins. Since that moment, all the following rulers of Milan were styled as dukes.
After the death of Filippo Maria in 1447, the main line of Visconti went extinct. Benefited by political chaos, a cabal of wealthy citizens, academics and clerics declared the Duchy dissolved and proclaimed the oligarchical Golden Ambrosian Republic. The republic was never recognized and the neighboring states of Venice and Savoy tried to expand their fiefdoms in Lombardy, as well as France. Taking advantage of the state's weakness and the resurgent Guelph-Ghibelline conflict, the commander-in-chief of the Milanese forces, Francesco I Sforza, defected from Milan to Venice in 1448, and two years later, after several side switches and cunning strategies, Sforza entered the city during Annunciation. He was then declared the new Duke of Milan, using as a claim his marriage with Bianca Maria Visconti, illegitimate daughter of Filippo Maria.
In 1494, Ludovico Sforza usurped the throne of Milan, after probably poisoning his nephew Gian Galeazzo. After Venetian's threats, Ludovico solicited French kingCharles VIII to descend into Italy, starting the First Italian War. After Ludovico's betrayal and alliance with League of Venice in 1495, French were defeated in the Battle of Fornovo and unabled to expand in Italy. Charles VIII's top general and cousin, Louis II, Duke of Orléans, was humiliated and due to his personal hate toward Ludovico Sforza, started to claim the Duchy of Milan for himself, quoting his paternal descendance from Valentina Visconti and Gian Galeazzo's last will. After Louis XII's ascension to the French Throne in 1499, he started the Second Italian War to conquer Milan and Naples. With French armies near Pavia, Ludovico and his loyalists left Milan on 17 September 1499 to flee toward Germany. This left Louis XII as only Duke of Milan, entering in city on 6 October 1499.
Ludovico Sforza was captured on February 1500, dying in hard prison in 1508. His son Massimiliano became the Sforza claimant to the Milanese Throne, finally re-gained in January 1513, six months after the Swiss army entrance in Milan.
After their defeat in the Battle of Marignano in 1515, the Swiss retired from Milan and Massimiliano was imprisoned by the returning French troops. He waived his rights to Milan for the sum of 30,000 ducats and continued to live in France.
By November 1521, the French situation had deteriorated considerably. Emperor Charles V, Henry VIII of England, and the Pope Leo X signed an alliance against Francis on 28 November. Odet de Foix, Viscount of Lautrec, the French governor of Milan, was tasked with resisting the Imperial and Papal forces; he was outmatched by Prospero Colonna, however, and by late November had been forced out of Milan and had retreated to a ring of towns around the Adda River. For the third time and last time, Sforza were restored to power.
In 1535, after the death of the heirless Francesco II Sforza, Emperor Charles V annexed the Duchy as a vacant imperial state in order to avoid other claims by the French or the collateral branches of Sforza.
In September 1700, Charles became ill; by 28 September he was no longer able to eat and Portocarrero persuaded him to alter his Will in favour of Louis XIV's grandson, Philip of Anjou. When Charles died on 1 November 1700, the throne was offered to Philip, who was proclaimed King of Spain on 16 November 1700. This was accepted by Britain and the Dutch Republic among others but disputes over division of territories and commercial rights led to the War of the Spanish Succession in 1701.
After the Treaty of Rastatt of 1714, Emperor Charles VI officially gained the Duchy of Milan, a possession considered vital to the security of Austria's southern border. Since that moment, Milan was a permanent possession of Austrian branch of Habsburg Monarchy.