The Senate overwhelmingly gave final approval early Saturday to a $1.2 trillion spending bill to fund more than half of the government, effectively averting a shutdown by sending the legislation to President Biden’s desk just hours after a midnight deadline.

The 74-to-24 vote, which concluded about 2 a.m., capped an extraordinary day on Capitol Hill that began with a big bipartisan vote to speed the measure through the House, which set off a conservative revolt and prompted one Republican to threaten a bid to oust Speaker Mike Johnson from his post.

The Senate action came more than 12 hours after the House vote, after intense haggling to arrange a series of politically charged votes on proposed changes to the legislation that Republicans had demanded that threatened to push the government into a brief partial shutdown into the weekend.

The White House said in a statement minutes after the midnight deadline that federal officials had “ceased shutdown preparations” in anticipation of Mr. Biden signing the legislation later Saturday. But the delay underscored the difficulties that have plagued spending negotiations from the beginning and was a fitting coda to an excruciating set of talks that are on track to finally fund the government through the end of the fiscal year, Sept. 30, six months behind schedule.

“It’s been a long day, a long week and a very long few months,” said Senator Chuck Schumer, Democrat of New York and the majority leader. “But tonight, we have funded the government with significant investments for parents and kids, and small businesses and health care workers, military families, and so much more. It’s no small feat to get a package like this done in divided government.”

Earlier on Friday, in a 286-to-134 vote that came down to the wire in the House as leaders scrounged for the two-thirds majority needed for passage, Democrats rallied to provide the support to overcome a furious swell of opposition by conservative Republicans.