Elon Musk is fed up with California.

The billionaire entrepreneur said Tuesday he was relocating the headquarters of two of his companies, X Corp. and SpaceX, to Texas from California. The moves, announced days after he endorsed Donald Trump for president, further illustrate how Musk has increasingly aligned himself with conservative stances on social issues.

His disclosures followed the move by California Gov. Gavin Newsom , a Democrat, to sign a new law Monday that aims to prevent schools from informing families if their children identify as gay or transgender.

“This is the final straw,” Musk wrote on X , the social-media platform he owns. He cited the law as well as “many others that preceded it, attacking both families and companies.”

The California law prohibits school districts from requiring employees to disclose information about a student’s sexual orientation or gender identity without the student’s consent. Proponents of the law say it protects children from being forced into being outed and creates a safe place for them in school. Critics say it infringes on the rights of parents to be informed.

“I did make it clear to Governor Newsom about a year ago that laws of this nature would force families and companies to leave California to protect their children,” Musk said.

Newsom, writing on X on Tuesday evening, referred to a post Trump made on social media two years ago. The former president said back then that Musk would have got on his knees and begged him for government subsidies if Trump had asked him to.

“You bent the knee,” Newsom said Tuesday.

The official X account for Newsom’s press office also chimed in .

“The last time Elon Musk ‘moved’ an HQ, Tesla ended up expanding in California—even relocating their Global Engineering & AI headquarters to California because of our diverse, world-leading talent,” the post said.

SpaceX didn’t respond to a request for comment. An X spokesman declined to comment.

Musk on Saturday formally threw his support behind Trump shortly after the former president survived an assassination attempt at a rally in Pennsylvania. The Wall Street Journal on Monday reported that Musk has said he plans to commit around $45 million a month to a new super political-action committee backing Trump’s presidential run, citing people familiar with the matter.

On Tuesday, Musk said his rocket company, SpaceX, would move its headquarters from Hawthorne, Calif., to an area in Texas where SpaceX has been expanding its Starbase manufacturing and launch site, near Brownsville in the southeastern corner of the state.

He said X would move its headquarters from San Francisco to Austin, Texas.

“Have had enough of dodging gangs of violent drug addicts just to get in and out of the building,” he said about X’s current digs in San Francisco.

Texas Gov. Greg Abbott, a Republican, reposted one of Musk’s tweets on X and said that the SpaceX move “cements Texas as the leader in space exploration.”

Musk’s social-media company, X, previously known as Twitter, has been based in the Mid-Market neighborhood of San Francisco for more than a decade—long before Musk acquired the company in 2022.

Musk has criticized how San Francisco leaders have responded to problems such as crime. “Many Twitter employees feel unsafe coming to work in downtown SF and have had their car windows smashed,” Musk posted on his platform last year. “They also got such a null response from the police that they rarely even bother reporting crimes anymore, because nothing happens.”

Musk, however, also pledged last year that X would remain in San Francisco. “Many have offered rich incentives for X (fka Twitter) to move its HQ out of San Francisco,” Musk posted on X in July 2023. “Moreover, the city is in a doom spiral with one company after another left or leaving. Therefore, they expect X will move too. We will not.”

X has been expanding in Texas. Earlier this year, the company said it was hiring employees for a team based in Austin to focus on content safety issues.

It wasn’t immediately clear what will happen to X’s headquarters complex in San Francisco. X could keep a presence in the area. That could lead to a setup similar to the one at Musk’s electric-vehicle company, Tesla, which is based in Texas but has an office called its “engineering headquarters” in Palo Alto, Calif.

Before Musk’s announcement Tuesday, X was making plans to sublease excess space that it was no longer using at its headquarters in San Francisco following staff reductions, people familiar with the plans said last week.

As for SpaceX, the rocket company has long had a footprint in Texas, and has been taking steps to increase its presence there. The company has filed paperwork to reincorporate in Texas, from Delaware, where a judge earlier this year ruled against Musk’s pay package at Tesla.

In McGregor, Texas, a city located near Waco, SpaceX tests engines and other hardware. Outside of Austin, it opened a new factory spanning more than 700,000 square feet to produce Starlink user terminals. Along a Gulf of Mexico beach not far from Brownsville, the company has been pouring resources into what it calls Starbase .

Starbase, soon to be SpaceX’s headquarters, sprawls over several parcels of land near tidal flats and parks. The company said in 2023 it directly employed more than 1,800 people at Starbase, making it the largest employer in the area.

The complex, which SpaceX has called the “Gateway to Mars,” includes a launchpad for the company’s Starship rocket, a factory, housing and other facilities tied to the huge vehicle, which SpaceX is still trying to demonstrate can fly as designed.

More activity is expected at Starbase. Earlier this year, the company said it planned to develop a five-story office as an addition to its factory there, according to a filing from the Texas Department of Licensing and Regulation. It recently asked federal aviation officials for permission to launch Starship up to 25 times a year from Starbase, up from the five launches annually it currently has permission to operate.

SpaceX has won support from many members of the communities where it operates in Texas, but has generated pushback and frustration, too. More than a year ago, dozens of residents of Bastrop County took issue with SpaceX’s Starlink factory and expansion plans from Musk’s Boring Co., a tunneling firm.

SpaceX, meanwhile, frequently closes down the beach adjacent to Starbase for Starship operations, and some environmentalists and residents say regulators haven’t done enough to mitigate the effect the operations are having on land and wildlife. The company has said it is proud to be an active part of the community near Starbase, touting its work in the region.

Shifting headquarters to Starbase from Hawthorne, Calif., marks at least a symbolic end of an era for the company. SpaceX, founded by Musk more than two decades ago, grew up in Southern California.

It occupies a large factory complex in Hawthorne, a city just southeast of Los Angeles International Airport, and has a significant workforce there. A towering SpaceX booster sits near one entrance of the building.

After X and SpaceX move, Musk’s brain-chip startup, Neuralink, and his xAI venture will be the only two of his companies still with headquarters in California.

Write to Joseph Pisani at joseph.pisani@wsj.com , Alexa Corse at alexa.corse@wsj.com and Micah Maidenberg at micah.maidenberg@wsj.com