Spain originally began controlling Gibraltar in 1501. But the Rock then came under British control during the War of Spanish Succession in 1704. Then in 1779 Spain tried but failed to win it back in what was known as the Great Siege of Gibraltar.
The rock was declared a colony in 1830.
https://www.thesun.co.uk/news/3240916/gibraltar-spain-brexit-overseas-territory-veto-rock/