According to legend, in 1139 Count Afonso Henriques won a decisive victory against Moorish forces at Ourique. The five shields he supposedly struck from the hands of five Moorish kings were later reflected in the five blue shields on his white banner. Each shield bore five white disks for the five wounds of Christ, who, according to tradition, appeared before the battle and guaranteed the count’s success. In the 13th century King Afonso III added a red border with gold castles to the shield as a symbol of the neighbouring kingdom of Castile; this may have occurred in 1254, when he married Beatriz of Castile (the illegitimate daughter of Alfonso X) and arranged for the territory known as the Algarve to be transferred to Portugal.
Surmounted by a crown, those arms appeared on many Portuguese flags over the centuries—for example, after 1640, when Portugal regained its independence from Spain, its flags were white with the royal arms. In 1816 a symbol for Brazil, the armillary sphere, was added behind the shield. The armillary sphere was used as a navigational instrument by previous Portuguese kings who had sponsored worldwide voyages of exploration and settlement in the 15th and 16th centuries. Although that symbol was abandoned in the 1820s, when Brazil became independent, it was revived on June 30, 1911, following the October 1910 revolution that overthrew the monarchy and made Portugal a republic. At the same time green and red replaced the blue and white background stripes of the flag, in use since 1830. Red was the colour of the revolutionary flag, and green was added so that the new national flag would not be confused with the old royal standard, which had a plain red background. Green and red also had been featured in many early Portuguese flags, such as those of the Order of Christ and those showing the crosses of Avis.