Thanks to the scholar’s studies dating back to the 18th century we are now able to reconstruct fairly accurately the historical phases of San Leo, which has often often changed its name over the centuries.
In the case of San Leo, studying the hisitory of the name tells us the history of the city, so we will start with the etymology...
Origins of the name San Leo (and earlier names)
In ancient times the Romans built a temple here dedicated to Jupiter, 'Feretrus', for which the place was called "Mons Feretrus" ('Mount Feretrus’), hence the modern term Montefeltro. It was then called San Leo, from 'Leo-ne', who founded a place of Christian worship here in the 4th century AD.
Regarding the definition of Jupiter as "Feretrus", this cult was instituted for the first time in Rome after the Romulus’ (771-715 BC) victory against the Ceninensian. The term Feretrus has been widely discussed, but is usually accepted that it derives from the Latin verb "Ferire", "strike", hence 'Jupiter Feretrus' would be a 'striking Jupiter'.
We do not know with absolute precision when San Leo ceased to be called Mons Feretrus, but it is likely that the name change occurred during the 9th-10th century AD. In the 18th century Giambattista Marino made a very detailed study  about San Leo and referred to some interesting documents in Latin about this subject, and based on a quote from Leone Ostiense (1046-1115) [Leo of Ostia]; it seems that at the time of Berengar II (900-966) the town was called San Leo:
"[...] Otto, after having besieged Berengar in the castle called ‘Monte Leone’, in the past [called] 'Mons Feretrius' and now ‘Montefeltro’, made him prisoner [...]"
This episode refers to the siege that the Emperor Otto I (912-973) of Saxony put on the castle of Saint Leo, where Berengar II, Marquis of Ivrea and King of Italy, was locked up. The incident occurred in 962, so, around that date the town was being referred to as San Leo, and probably for a long time before - so much so that Leone Ostiense says that San Leo was called Mons Feretrius "alias" i.e. "once", "long ago".
In fact, Giambattista Marino says that, according to certain papal documents, in the 8th century San Leo was still called Monteferetro Castle.
Let us add that, in ancient times, San Leo was also referred to as "Mons Feretrus" and "Mons Felicianus" (Italian: "Monte Feliciano"). This is also explained by Giambattista Marino, who points out that:
"over the cliff on which the little town is built there are two mountains, a top to the East, and a lower one to the West - on the East stood the Temple of Jupiter "Feretrus", so called "Monteferetro"; the other, no one knows why, says Giambattista Marino, was called "Monte Feliciano" *.
* Giambattista Marino reports as many as eight different names for St. Leo: "Mons Feretrus", "Feretrius", "Feretranus", "Fanum S. (ancti) Leonis ("Temple of St. Leon"), "Leopolis" ("The city of Leon"), "Sanctus Leo", "Monteferetro", "San Leone" and, finally,"San Leo".
Apart from the episode mentioned with the protagonists Otto I of Saxony and Berengar II in the second half of the 10th century, there is insufficient documentation for the late Middle Ages to reconstruct the history of San Leo, though presumably, as Giambattista Marino pointed out, the place was fought over among the great feudal Lombards, Franks and Germans.
San Leo from the Middle ages onwards
Our knowledge improves in the Middle Ages, when San Leo became a fief of the powerful family of the Carpegna, who got the feud from Frederick I of Hohenstaufen (1122-1190).
The Carpegna subsequently became Count of Montefeltro and Duke of Urbino, and moved to Urbino. On that occasion, Saint Leo became a municipality, but soon fell under the domination of the Family of Tiberti, then of the Church State (with Cesare Borgia [1475-1507]) in 1503. It subsequently passed to Francesco Maria I della Rovere [1490-1538] in 1517, followed by the Malatesta family and then the Dukes of Urbino and Florence.
So eventually the town became part of the Papal States, and later part of Italy after the unification of the Kingdom of Italy in 1861. Today San Leo is a city that, thanks to its intact historical and artistic heritage, has managed to increase tourism by offering great services.
See the travel information for San Leo.
1. See the "Saggio di ragioni della città di Sanleo, detta già Monteferetro" ["Essay of the reasons for city of Sanleo, called already Monteferetro", Pesaro, 1758