President Donald Trump spoke following the House passage of the Americana Health Care Act. Video by PBS NewsHour.

WASHINGTON — Relieved Republicans muscled their health care bill through the House Thursday, taking their biggest step toward dismantling the Obama health care overhaul since Donald Trump took office. They won passage only after overcoming their own divisions that nearly sank the measure six weeks ago.

Beaten but unbowed, Democrats insisted Republicans will pay at election time for repealing major provisions of the law. They sang the pop song “Na Na Hey Hey Kiss Him Goodbye” to the GOP lawmakers as the end of the voting neared. The Republicans seemed unworried, many of them busing to the White House for a victory appearance with the president.

The measure skirted through the House by a thin 217-213 vote, as all voting Democrats and 20 mostly moderate Republican holdouts voted no. A defeat would have been politically devastating for President Trump and Speaker Paul Ryan, R-Wis.

Passage was a product of heavy lobbying by the White House and Republican leaders, plus late revisions that nailed down the final support needed. Leaders rallied rank-and-file lawmakers at a closed-door meeting early Thursday by playing “Eye of the Tiger,” the rousing 1980s song from the “Rocky III” film.

“Many of us are here because we pledged to cast this very vote,” Ryan said. He added, “Are we going to keep the promises that we made, or are we going to falter?”

The bill now faces an uncertain fate in the Senate, where even GOP lawmakers say major changes are likely. In a statement, Senate Majority Leader Mitch McConnell, R-Ky., called the House vote “an important step” to repealing Obama’s law and said, “Congress will continue to act on legislation to provide more choices and freedom in health care decisions.”

Republicans have promised to erase President Barack Obama’s law since its 2010 enactment, but this year — with Trump in the White House and in full control of Congress — is their first real chance to deliver. But polls have shown a public distaste for the repeal effort and a gain in popularity for Obama’s statute, and Democrats — solidly opposing the bill — said Republicans would pay a price in next year’s congressional elections.

“You vote for this bill, you’ll have walked the plank from moderate to radical,” said House Minority Leader Nancy Pelosi, D-Calif., warning Republicans that voters would punish them. “You will glow in the dark on this one.”

The bitter health care battle dominated the Capitol even as Congress sent Trump a bipartisan $1 trillion measure financing federal agencies through September. The Senate approved that bill 79-18 a day after the House passed it easily, heading off a weekend federal shutdown that both parties wanted to avoid.

Ryan canceled a March vote on the health care bill because disgruntled conservatives said the measure was too meek while GOP moderates said its cuts were too deep.

He abandoned a second attempt for a vote last week. As late as Tuesday The Associated Press counted 21 GOP opponents — one short of the number that would kill the measure if all Democrats voted no.

Over the past few weeks, the measure was revamped to attract most hard-line conservatives and some GOP centrists. In a final tweak, leaders added a modest pool of money to help people with pre-existing medical conditions afford coverage, a concern that caused a near-fatal rebellion among Republicans in recent days.

GOP No votes on health care bill: Biggs, Coffman, Comstock, Costello (PA), Dent, Donovan, Fitzpatrick, Herrera-Beutler, Hurd, Jones… — Lisa Desjardins (@LisaDNews) May 4, 2017

GOP 20 NO VOTES on healthcare (more): Joyce (OH), Katko, Lance, LoBiondo, Massie, Meehan, Reichert, Ros-Lehrinen, Smith (NJ), Turner. — Lisa Desjardins (@LisaDNews) May 4, 2017

The bill would eliminate tax penalties Obama’s law which has clamped down on people who don’t buy coverage and it erases tax increases in the Affordable Care Act on higher-earning people and the health industry. It cuts the Medicaid program for low-income people and lets states impose work requirements on Medicaid recipients. It transforms Obama’s subsidies for millions buying insurance — largely based on people’s incomes and premium costs — into tax credits that rise with consumers’ ages.

It would retain Obama’s requirement that family policies cover grown children until age 26.

But states could get federal waivers freeing insurers from other Obama coverage requirements. With waivers, insurers could charge people with pre-existing illnesses far higher rates than healthy customers, boost prices for older consumers to whatever they wish and ignore the mandate that they cover specified services like pregnancy care.

The bill would block federal payments to Planned Parenthood for a year, considered a triumph by many anti-abortion Republicans.

GOP candidates including Trump put repealing Obama’s statute at the top of their campaign pledges, contending it’s a failing system that’s leaving people with rising health care costs and less access to care.

Democrats defended Obama’s law, one of his crowning domestic achievements, for expanding coverage to 20 million Americans and forcing insurers to offer more generous benefits. They said the GOP measure would toss millions off coverage while delivering tax cuts to the wealthy.

The nonpartisan Congressional Budget Office estimated in March that the GOP bill would end coverage for 24 million people over a decade. That office also said the bill’s subsidies would be less generous for many, especially lower-earning and older people not yet 65 and qualifying for Medicare.

A CBO estimate for the cost of latest version of their bill was not ready before the House vote.

Earlier this week, moderates objected that constituents with pre-existing conditions could effectively be denied coverage by insurers charging them exorbitant premiums.

But GOP leaders seemed to win over a raft of wavering lawmakers after adding $8 billion over five years for state high-risk pools, aimed at helping seriously ill people pay expensive premiums. That was on top of $130 billion already in the bill for states to help customers, though critics said those amounts were insufficient.

The House overwhelmingly approved a second bill that Republicans wrote to snuff out a glaring political liability. The measure would delete language in the health care measure entitling members of Congress and their staffs to Obama’s coverage requirements, even if their home states annul them.

Associated Press writer Andrew Taylor contributed.

This is a developing story. PBS NewsHour will update the post once more details become available.

READ MORE: What’s in the House GOP health care bill?