BOSTON — When the Yankees were at their most desperate for a win — and a decent outing from a starting pitcher — it was Domingo German who provided.

Perhaps it shouldn’t have been a surprise, since German has been arguably their most reliable starter this season and he has given the rotation a much-needed boost while others have struggled.

On Sunday against the Red Sox, he became the first Yankee starter to finish five innings in the past eight games in a 9-6 win that prevented the Red Sox from completing a four-game sweep at Fenway Park.

“He showed great poise,’’ Aaron Boone said. “In a game we really wanted, he came out and set the tone and delivered the goods for us.”

German gave up three runs in 5 ¹/₃ innings and left with a runner on second. Tommy Kahnle retired the next two batters to protect what was a three-run lead.

After three nights of seeing the Red Sox knock Yankees pitching around, German didn’t give up a hit until there was one out in the fourth — and that came when Gio Urshela couldn’t come up with a short hop on a chopper by Xander Bogaerts.

German then struck out J.D. Martinez before Andrew Benintendi homered to right to cut Boston’s deficit to 4-2. It was just about the only mistake German made, as he walked just one and whiffed nine before leaving after 77 pitches, when Boone decided to hand the game over to the bullpen.

The performance came after his worst start of the year, when German struggled with his curveball and gave up a career-high eight runs in just 3 ²/₃ innings in Minnesota.

Prior to that, German had delivered three straight strong starts since returning from the injured list with a strained left hip flexor.

Still, he entered the game with a 5.72 ERA on the road this year and was making just his second career start at Fenway.

“I felt it was my responsibility to go out there and put a stop [to the losing streak],’’ German said through an interpreter. “It’s been a rough patch for our starters. I needed to go out, do my job and be aggressive.”

His mentality is what stood out the most.

“I’m not surprised that moment for him wasn’t too big at all,’’ Boone said. “I knew he’d be in command of his emotions, regardless of the outcome. And he certainly was.”

His catcher agreed.

“We needed some depth and that was the most calm I’ve seen him, the most collected I’ve seen him,’’ Austin Romine said. “I think he was on a mission. … He’s a confident man when he’s out there on the mound. I know it may not look like it.”

How much the Yankees can rely on the 26-year-old right-hander remains to be seen, since he’ll soon be up against an innings limit.

But on Sunday, he gave the Yankees just what they needed.