In somewhere mid-1840's--that's somewhere before the Gold Rush and after the independence and annexation of Texas--was The Bear Flag Revolt in California.
By the 1840's, more and more American settlers started to trickle in to Mexican California. General Jose Castro from the Mexican army had become restless seeing this subtly slow invasion by the Americans. At the same time, John Fremont, a dude set about to make maps for the U.S. had come into California despite lack of approval by Castro or Mexico. Along his wanderings, he was eventually met by a messenger from Washington that informed him of rising conflicts between the U.S. and Mexico. With that, Fremont headed through California, towards Sonoma and gathering a following to build an insurrection. Worried that Castro was in plots to exodize the American settlers out of California, it was easy to convince more followers. When at Sonoma, they placed General Vallejo--a prominent landowner who was given substantial portions of land by Mexico--under arrest. And there, they made a pretty little rag-tag flag with a bear (that looked like a pig), a star, and a red stripe. Thus was the Bear Flag Revolt.
Meanwhile, border disputes over Texas and which river American territory ended at was a-ruffle. When Mexican soldiers crossed the Rio Grande to assert its control over Mexico's believed border lines, America declared war. News reached Commodore Sloat, who had been given orders to take California (reminds me of a Propellerheads song) if war broke out. Heading to Monterey Bay, Sloat learned of what Fremont had done in Sonoma with the Bear Flag Party. Basically, California was an independent state (under the Bear Flag, for about one month!), and Sloat took the chance and declared California a state of the United States--much like how Texas was annexed.
Thus was The Bear Flag Revolt and the birth of the State of California.