House Minority Leader Nancy Pelosi (D-Calif.) and Senate Minority Leader Senator Harry M. Reid (D-Nev.). (Brendan Smialowski/AFP/Getty Images)

Nancy Pelosi had a plan. Democrats were outnumbered, obviously, and she no longer had the power to impose her will the way she did when she was speaker of the House. But neither did the current speaker, John A. Boehner (R-Ohio).

With a partial Department of Homeland Security shutdown looming, Pelosi saw a way to torpedo Boehner, and get exactly what she and other Democrats wanted for President Obama. The plan was simple: When Boehner needed her the most, she would not be there for him.

She explained her plan to Senate Minority Leader Harry M. Reid (D-Nev.) in a private meeting in Reid’s office. He concurred. She encouraged her caucus to reject Boehner’s proposal for a stopgap DHS funding bill, knowing that Boehner could not sufficiently rally his own caucus to pass the bill without Democratic help.

Five days later, Democrats got exactly what they wanted: DHS was fully funded without any rollback of Obama’s executive actions on immigration.

Four months after getting kicked to the curb in the November midterm elections, Democrats across the country are still licking their wounds, quarreling over the party’s direction and messily plotting their path back to power in 2016. But inside the halls of Congress, they aren’t acting like a dejected minority.

[Boehner’s problem isn’t just House Republicans. It’s House Democrats, too.]

Instead, House Minority Leader Pelosi (D-Calif.) and Reid have been deftly navigating the big legislative debates to maximum advantage, thwarting the new majorities’ early ambitions and protecting Obama from the GOP assaults on his agenda.

Their strategy? Stick together when the other side doesn’t. It sounds simple enough. But it wasn’t always clear to Democratic leaders that they could pull it off.

“When we first got back in January, everyone was pretty depressed,” said Sen. Charles E. Schumer (N.Y.), the third-ranking Senate Democrat. The party had just lost its Senate majority and watched Republicans claim their largest House majority in decades.

Republicans doubt Democrats can hold their members together as well in future debates in which there is more potential for fracturing. They charge that Democrats shut down debate over DHS funding and are moving to protect Obama’s agenda at all costs.

But Democrats see the DHS fight as a win they can build upon that had roots in a separate matter. In the Senate, the pivotal party unity moment came in late January during the debate over construction of the Keystone XL Pipeline, according to several Democrats. The weekend after Majority Leader Mitch McConnell (R-Ky.) opted not to hold votes on some Democratic amendments, Reid and his top deputies called rank-and-file senators to gauge whether they would have enough support to block the bill’s progress the following Monday.

What they found was Democratic frustration with the GOP’s tactics. They capitalized, keeping McConnell short of the 60 votes he needed to advance the measure under Senate rules. It was the first major blow to the unified Republican majority.

“We don’t have 60 votes. So as long as they stay together, they will have an influence,” said Sen. Lindsey O. Graham (R-S.C.).

Keystone passed later that week after Democrats got to voice the input they sought, and the bill later cleared the House. But Obama vetoed the measure, and Senate Democrats prevented Republicans from overriding him.

[Obama’s Keystone veto will stand after Senate vote fails]

In the House, the DHS funding debate united a Democratic caucus that had shown signs of discord. Last December, tensions between liberals and moderates flared up over provisions tucked into a $1.1 trillion government funding measure that made a concession to large banks and enhanced the power of wealthy political donors.

A crucial day was Feb. 26, when Pelosi and Reid huddled in Reid’s office to talk strategy just before a joint news conference. Later that day, Pelosi and her top deputies agreed to ask rank-and-file Democrats to hold their votes until the end on a DHS funding bill House Republicans were about to unveil.

“Let’s enhance our own power in this thing, because this is a culmination of a lot of things coming together, whether it’s immigration, whether it’s anti-Obama,” Pelosi said in a meeting with members the next morning, according to Democrats in the room. She said Reid was on board but that if the House didn’t stop it, the bill could pass the Senate.

Boehner sold his three-week stopgap bill to Republicans as a way to avoid a looming DHS shutdown and continue to push for a longer-term funding bill that would also fight Obama on immigration. A combination of dozens of conservative Republicans who weren’t sold on Boehner’s plan and all but 12 Democrats who voted kept it from passing.

“When it became obvious that the Republicans were divided,” said House Minority Whip Steny H. Hoyer (D-Md.), Democrats saw a “real opportunity to defeat the Republican strategy.”

Hoyer and his whip team approached the Democratic rank-and-file within an hour of concluding Republicans would release a three-week bill, according to a Democratic leadership aide. They reached out to members on the House floor as well as through phone calls and staff, said the aide, who was granted anonymity to describe strategy.

The Senate had passed its own long-term DHS funding bill with no immigration provisions that same day. By the evening, Reid had already gone home to the Ritz-Carlton — he would later return — and Democratic Senate aides were bracing for a long night over dinner from Chipotle.

But after a flurry of phone calls involving top congressional leaders, Democrats say they became convinced that if they helped pass a one-week stopgap bill, they would get a House vote on the “clean” Senate bill.

Boehner’s office denies he ever guaranteed that would happen. But Democrats whipped votes for the one-week bill with the promise that it would give them what they sought. It passed.

Then on Tuesday, the House passed the Senate’s “clean” bill. Every House Democrat who voted supported it.

Democrats’ unity-above-all-else strategy isn’t exactly new. Boehner and McConnell took a similar approach six years ago when their caucuses were relegated to their smallest minority status in a generation. Boehner got every House Republican to oppose Obama’s stimulus plan, and McConnell did the same on his health-care law. The hope was to cast the legislation as sharply partisan.

Democratic leaders are hoping to head off future dissent by reminding the rank-and-file of what they saw in the DHS standoff: They hold the cards in big fights when they are united and Republicans are in disarray.

“We’re learning pretty quickly how to operate in the minority on key votes,” Schumer said.

sean.sullivan@washpost.com

Paul Kane contributed to this report.