RICHMOND — Virginia Democrats nearly completed their reshaping of state government on Saturday, wrapping up most of a historic General Assembly session in which they shattered 20 years of Republican leadership on social and environmental issues.

The state budget was the primary area left unfinished, preventing the 60-day legislative session from ending as scheduled Saturday after an avalanche of last-minute dealmaking.

Lawmakers raced to put together a deal to extend the session into Sunday to finish working on bills, and then to return later in the week to complete the two-year, $135 billion budget. Rules require the budget to sit before lawmakers for 48 hours before voting so they and the public have time to digest it.

Less than half an hour before midnight, the House and Senate reached a deal to extend the session. They set 6 p.m. Sunday as a deadline to finish action on all bills except the budget, and agreed to return Thursday to consider the budget.

Late Saturday, just outside the doors of the Senate, House Minority Leader Thomas K. Norment Jr. (R-James City) made an offer to House Majority Leader Charniele L. Herring (D-Alexandria): Republicans would agree to the extension, allowing them to complete work on Sunday, and then come back Thursday to finish the budget. In return, Norment said, the House would have to stop holding up an anti-gerrymandering constitutional amendment, which the chamber has passed but not yet “transmitted” to the Senate. Herring then said she would run that past Speaker Eileen Filler-Corn (D-Fairfax).

Swept by elections last fall to their first consolidated hold on power in a generation, Democrats unleashed a cyclone of pent-up legislative change. On Saturday, lawmakers completed action on measures to transition to 100 percent renewable energy by 2045, give undocumented immigrants a way to drive legally and repeal the requirement that voters show photo identification at the polls.

Other big-ticket items went unresolved by early evening, including bills that would decriminalize marijuana, legalize casino gambling in five cities, give localities the authority to remove Confederate statues and raise the state’s minimum wage. In some cases conference committees were hashing out differences between House and Senate versions; in others, bills were caught up in procedural spats.

Many of the delays were a consequence of a backroom fight over the budget. In particular, the Senate was refusing to go along with a House plan to freeze tuition at public colleges and universities for the coming year. House Democrats retaliated by refusing to “transmit” to the Senate its version of a proposed constitutional amendment to set up a redistricting commission. Until the measure is sent back to the Senate, the House could try to bring it back up to reconsider the vote.

“All that’s going to do is make us hold up their bills,” Sen. L. Louise Lucas (D-Portsmouth) said, meaning that the Senate would stop sending conference reports on House measures until the stalemate ended. “They’re not going to get one thing out of here.”

The deal for the extension had appeared to be wavering as the House was reluctant to send over the amendment resolution. Senate Majority Leader Richard L. Saslaw (D-Fairfax) came to the House chamber, fuming, and told Herring that if the resolution didn’t reach the Senate within five minutes, collective-bargaining and minimum-wage bills were dead. “If you think I’m bluffing, try me!” he said as he left.

The showdown didn’t affect action on gun violence prevention, which was the session’s marquee issue from start to finish. On Saturday, bills restricting handgun purchases to one per month and expanding background checks cleared the legislature and headed to the desk of Gov. Ralph Northam (D) for his signature.

They were the last of seven gun-control measures advocated by Northam that passed the legislature after years of such bills being quashed by Republican majorities. An eighth part of Northam’s package — a ban on assault weapons — cleared the House but never made it through the Senate.

A record number of bills introduced this year led to complaints from Republicans that new leadership was trampling on parliamentary customs in a rush to shake things up.

The scope of change caused backlash. Thousands of gun rights advocates from around the country protested in Richmond in January, some making threats that drew a massive police response. Democrats banned guns in the Capitol, and residents of rural areas complained that their way of life was under attack.

Soon after, national women’s rights groups celebrated Virginia becoming the 38th state to pass the Equal Rights Amendment. Lawmakers went on to extend new LGBT protections, remove restrictions on access to early-term abortions and make it easier to vote — including making Election Day a holiday and canceling a holiday honoring Confederate generals.

The undoing of so many Republican priorities led some conservatives to advocate giving blue parts of Northern Virginia back to the District of Columbia or ceding red counties to West Virginia.

Democrats defended their agenda as carrying out a mandate from last fall’s elections, which gave the party majorities of 55 to 45 in the House and 21 to 19 in the Senate.

“I’m very proud of the work that the House has done in delivering on the promises that we made,” Del. Marcia S. “Cia” Price (D-Newport News) said. Even more than specific bills, Price said she was proud of “the philosophy of putting people first that we took to the legislative process.”

The elections gave women and minorities unprecedented gains in leadership. Several female lawmakers paused during Saturday’s session to note Corn’s status as the first woman to serve as House speaker in the body’s 401-year history.

“For being an example for all the young girls, for everybody who thought they’d never see it, we appreciate you,” Del. Jeion A. Ward (D-Hampton) said.

The remarks sparked applause on both sides of the aisle — a moment of accord on a grueling and partisan day.

Dealmaking continued down to the wire, even on gun-control bills that have been percolating since the General Assembly convened on Jan. 8.

The background-check measure hit a snag earlier in the session, with the Senate taking a more conservative stance than the House. The Senate wanted background checks to be required only for firearms purchases, while the House wanted them on sales and transfers.

The Senate’s version prevailed in committee, and both chambers voted to approve the deal. The final vote came in the Senate shortly after noon on Saturday.

The Senate also won out in a tug of war over the gun-purchase-limit measure, getting the House to agree to exempt concealed-carry permit holders from the limit. Another gun-related bill heading to the governor would require home day-care facilities that register with the state to keep firearms unloaded and locked up when children are present.

Also voted out Saturday were bills to halt the practice of suspending driver’s licenses for failing to pay fines and to give local governments the option to impose a five-cent tax on disposable shopping bags.

Another bill on its way to Northam would lift a requirement that jailers report all undocumented immigrants charged or convicted of any crime to federal immigration authorities. Under the version that passed, jailers still would have to report felonies but not misdemeanors. They would still have the option to report those arrested on misdemeanors.

The House and Senate approved legislation that would make a limited class of inmates eligible for parole, which was abolished in 1995. The bills would apply to 310 inmates sentenced by juries who were not told about Virginia’s parole ban during the five years before a 2000 state Supreme Court decision requiring that notification.

In a surprise move, the Senate shot down a transportation safety bill that would have allowed police to pull over a vehicle if a driver or any passengers were not wearing seat belts. Current law only requires seat belts for drivers, front-seat passengers and children.

The bill would have extended the requirement to all adult passengers and make not wearing a seat belt a primary offense — meaning police would be able to make the stop solely for that reason. Democrats and Republicans raised concerns that the measure would be a pretext for police to target more drivers who are young and ethnic minorities.

Budget negotiations continued behind the scenes on Saturday. Del. Luke E. Torian (D-Prince William), chairman of the House Appropriations Committee, said conferees working with the Senate had made progress on areas related to spending for public education, commerce and natural resources, public safety and general government.

But disagreements between House and Senate negotiators persisted in areas of higher education, compensation for public employees and capital spending plans. Democrats were seeking to extend the session to continue looking for common ground.

Party caucuses met behind closed doors for more than an hour Saturday night to talk about how to proceed. They returned to the floor about 9 p.m. without indicating that they had settled on a way forward.