This timeline traces the history of Gibraltar from prehistoric times up to the modern era.

Gibraltar has a political and colonial history spanning more than five centuries, with evidence of human habitation dating back at least 20,000 years. During the Middle Ages, it was ruled by a succession of Muslim states before coming under Spanish control and later by the Spaniards. In 1713, Gibraltar was ceded to the United Kingdom, and its ownership has remained a matter of controversy ever since, largely due to its strategic location. Spain continues to claim sovereignty over Gibraltar to this day.

Prehistoric era

Evidence of hominid habitation of Gibraltar dates back to the age of Neanderthals. A 50,000-year-old Neanderthal skull was discovered in Forbes' Quarry in 1848, predating the credited discovery of the species in the Neander Valley in Germany in 1856. In 1926, the skull of a Neanderthal child was found inside the Devil's Tower.

Archaeological evidence of Neanderthal Mousterian Culture deposits found at Gorham's Cave has been dated to between approximately 26,000 and 22,000 BCE. This has led to suggestions that Gibraltar was one of the last places inhabited by the Neanderthals prior to their extinction.

Gibraltar's prehistoric ecology consisted of various species of rare flora and fauna, many of which have since gone extinct. According to biologist Clive Finlayson of the Gibraltar Museum, the "natural richness of wildlife and plants in the nearby sandy plains, woodlands, shrub lands, wetlands, cliffs, and coastline, probably helped the Neanderthals to persist."

Archaeological evidence from Gorham's Cave indicates that the Neanderthals of Gibraltar likely used it as a shelter over a period of ~100,000 years. This discovery challenges conventional assumptions about the timeline of the Neanderthal extinction, traditionally thought to have coincided with the arrival of early modern humans in Western Europe.

Ancient and Classical Period

Around 950 BCE, Phoenician-Mediterranean seafarers visited the area surrounding the Rock of Gibraltar, which they called "Calpe". The Carthaginians, themselves descended from the Phoenicians, are also known to have visited the Rock, although neither group appears to have ever established a permanent settlement.

The area now known as Gibraltar was also known to the Greeks. The Athenian philosopher Plato considered Gibraltar one of the Pillars of Hercules, along with Jebel Musa and Monte Hacho on the African side of the Strait of Gibraltar. The Romans visited Gibraltar during the 2nd century, but like earlier seafaring visitors, were either uninterested or unable to establish any kind of permanent settlement.

Following the fall of the Western Roman Empire and Germanic Migration Period, Gibraltar was settled first by the Vandals and later the Visigoths. Although the Visigoths remained, the Vandals did not remain for long. The Visigoths remained in the Iberian Peninsula from 414 CE to 711 CE.

The era of Visigothic rule in Southern Iberia was interrupted by a brief period of Byzantine control following the conquests of Justinian I during the mid 6th century. However, in the early 7th century, the Visigoths successfully reconquered the area and expelled the Byzantines from Iberia.

Moorish period

Due to its location on the shore of the Strait of Gibraltar, the area experienced frequent changes in control during this period. Beginning with Tariq ibn Ziyad’s landing in 711 CE, the Moors would begin a presence in Iberia lasting over seven centuries. During this period, the area known as Al-Andalus (Arabic: الأَنْدَلُس) was the subject of much political intrigue, bitter infighting, and open warfare between competing Moorish factions vying for control, the most notable and powerful being the Almohads, Nasrids, and Marinids. They would later be joined by the Christian kingdom of Castile in the north, which in the late Middle Ages would gradually grow in strength before launching the Reconquista, a prolonged military campaign by Christian kingdoms aimed at gaining control over the Iberian Peninsula and expelling Moorish influence. The following events illustrate Gibraltar's involvement in various regional conflicts over time. 30 April 711 – Seeking to conquer Spain, the Muslim Umayyad general, Tariq ibn Ziyad, sails across the Strait from Ceuta, in present-day Morocco, with an army of mainly Muslim Berber soldiers. He initially attempts to land on Algeciras but fails to do so and subsequently withdraws. On his second attempt, again departing from present-day Morocco, Tariq landed at the southern point of the Rock without being reported by local forces. The modern name of Gibraltar originates from the rock's Arab name, Gabal-Al-Tariq ("the Mountain of Tariq"). Little was built during the first four centuries of Moorish control. 1160 – The Almohad Sultan Abd al-Mu'min orders the building of a permanent settlement to be protected by a castle. It was called of Medinat al-Fath (English: "City of the Victory"). Upon completion of the town and fortifications, the Sultan crossed the Strait to inspect the works and remained in Gibraltar for two months. The Tower of Homage, part of the original castle fortifications, remains a preserved structure. 1231 – After the collapse of the Almohad Empire, Gibraltar is captured by Ibn Hud, Taifa Emir of Murcia. 1237 – Following the death of Ibn Hud, Gibraltar changes hands again as his domains are handed over to Muhammad ibn al-Ahmar, the founder of the Nasrid Emirate of Granada. 1274 – The second Nasrid king, Muhammed II al-Faqih, cedes Gibraltar to the Marinids as payment for their help in wars against the Christian kingdoms. 1309 – While King Ferdinand IV of Castile lays siege to the nearby port of Algeciras, Alonso Pérez de Guzmán (known in the Spanish records as Guzmán el Bueno) is dispatched to capture Gibraltar, resulting in the first siege of Gibraltar. The Castilians manage to seize the Upper Rock and utilized the elevated position to launch attacks on the town. The Moorish garrison surrendered one month later. In the early 14th Century, Gibraltar had a population of approximately 1,500 inhabitants. 31 January 1310 – King Ferdinand IV grants Gibraltar its first royal charter. This charter included various incentives to settle in the city, such as the offer of clemency for (some) past crimes to anyone who lived in Gibraltar for one year and one day. This event marked the establishment of the Gibraltar council. 1316 – Gibraltar is unsuccessfully put under siege by the Azafid Qaid Yahya ibn Abi Talib, an ally of the Nasrid Emirate of Granada. June 1333 – A Marinid army, led by Abd al-Malik, the son of Marinid sultan Abul Hassan, recovers Gibraltar after a five-month siege. King Alfonso XI of Castile immediately attempts to retake Gibraltar with a fleet commanded by Castilian Admiral Alonso Jofre Tenorio. During the ensuing fourth siege, the Marinid defenders dig a ditch across the isthmus. The siege lasts until August, when the Castilians were attacked by a Nasrid relief force from Granada. As a result, the siege ends in a truce, allowing the Marinids to keep Gibraltar.

March 1344 – Following a two-year siege beginning in 1342, Algeciras is conquered by Castilian forces. Because of this, Gibraltar becomes the main Marinid port in the Iberian Peninsula. During the siege, Gibraltar is used by the Marinids as a base to supply the besieged city. 1349 – Gibraltar is again unsuccessfully besieged by King Alfonso XI of Castile. 1350 – Alfonso XI once again attempts to put Gibraltar under siege. The siege fails following the arrival the Black Death, which kills Alfonso and eradicates much of the Castilian army. 1369 – The Castilian Civil War ends with the murder of King Peter I by the pretender Henry, who takes the regal name Henry II. Afterwards, the Nasrid king of Granada, Muhammad V, a former ally of Peter, attacks and captures over Algeciras following the successful three-day Siege of Algeciras. 1374 – Amid internal instability in the Marinid Sultanate of Fez, its Sultan Abu al-Abbas Ahmad al-Mustansir requests assistance from Muhammad V. Either as a condition of the alliance or as reward for Muhammad's successful expedition in Africa, the Marinids cede Gibraltar to the Nasrids of Granada. 1379 – The Castilians attack Algeciras. With its capture seemingly imminent, Muhammad V razes the town and renders its harbour unusable. As a result, Gibraltar's significance in the strait trade increases significantly. Subsequently, Muhammad and Henry sign a truce and the Nasrids retain control of Gibraltar. 1410 – The garrison in Gibraltar mutinies against the King of Granada and pledges its allegiance to the Marinid King of Fez, Fayd. Fayd sent his brother, Abu Said, across the Strait to Gibraltar to take possession of the city. Fayd also to seizes control of other Nasrid ports such as Marbella and Estepona. 1411 – The son of Yusuf III of Granada, Ahmad, recovers Marbella and Estepona from the Marinids and successfully lays siege to Gibraltar, resulting in its reconquest for the Kingdom of Granada. 1436 – Enrique de Guzmán, the second Count of Niebla and a landowner with significant holdings in southern Andalusia, launches an assault on Gibraltar. However, his attack is repelled and the Castilian forces incurred significant casualties.

Castilian and Spanish period

20 August 1462 – Castilian forces again capture Gibraltar following the eighth siege of Gibraltar. A dispute immediately breaks out between the House of Medina Sidonia (the Guzmán family) and the House of Arcos (the Ponce de León family) over the possession of the town. Juan Alonso de Guzmán, 1st Duke of Medina Sidonia, eventually succeeds and takes possession of the town as his personal property. However, the King of Castile, Henry IV, declares Gibraltar to be crown property, and not the personal property of the Guzman family. Henry IV would restore the charter granted to Gibraltar in 1310 as well as two additional measures: the lands formerly belonging to Algeciras were granted to Gibraltar; and the status of collegiate church was solicited from pope Pius II. The Holy See accepted, granting collegiate status to the local parish church, Saint Mary the Crowned (), now the Cathedral of St. Mary the Crowned, located on the site of the old main Moorish Mosque. St. Bernard of Clairvaux was designated as the patron saint of Gibraltar during this period. 1463 – In a tour through Andalusia, Henry IV is the first Christian monarch to enter the town of Gibraltar. July 1467 – In the midst of a noble revolt against the King, the forces of the Duke of Medina Sidonia take Gibraltar after a 16-month siege. Alfonso of Castile, half-brother of Henry IV and pretender supported by the rebelling nobles, granted him the Lordship of Gibraltar, resulting in the ninth siege of Gibraltar. 3 June 1469 – After the death of the pretender Alfonso, the 1st Duke of Medina Sidonia, his son and heir Enrique de Guzman, later 2nd Duke of Medina Sidonia, switches sides to the legitimist faction. Queen Isabella I of Castile rewards them by confirming the status of Gibraltar as a domain of the dukes of Medina Sidonia. 20 December 1470 – A new charter is granted to the town of Gibraltar, now a nobiliary town, based on the Antequera charter. 30 September 1478 – The crown grants the title of Marquis of Gibraltar to the Duke of Medina Sidonia. 20 January 1479 – Ferdinand of Aragon, husband of Isabella I, ascends to the throne of Aragon as Ferdinand II. Known as the Catholic Monarchs, they jointly rule the formerly rival kingdoms of Castile and Aragon. 31 March 1492 – Following their conquest of Granada, the Catholic Monarchs sign the Alhambra Decree, ordering the expulsion of the Jews from Spain, to take effect from 31 July 1492. Scores of refugees pass through Gibraltar on their way into exile in North Africa.