Didi Gregorius #18 of the New York Yankees celebrates his...

Didi Gregorius #18 of the New York Yankees celebrates his ninth inning game winning two run home run against the Texas Rangers with his teammates at Yankee Stadium on Wednesday, June 29, 2016. Credit: Jim McIsaac

It was CC Sabathia on Tuesday, with the reasonable voice of an elder statesman, who said now was the time for the Yankees to play with urgency. Before that, it was Brian Cashman who said these were the days that would determine the Yankees’ identity at the trade deadline and beyond.

“You can’t mess around too long and expect to be there,” Joe Girardi said Wednesday. “As the days tick away, you start to run out of time and you don’t want to bury yourself.”

Message received. In the biggest of ways.

The Yankees rallied in stunning fashion, scoring six runs in the bottom of the ninth to beat the first-place Texas Rangers, 9-7. Brian McCann’s three-run shot tied it at 7. Two batters later, Didi Gregorius ended it with a two-run walk-off homer.

The Yankees avoided their fourth straight loss and the win breathed life into a team on the brink of desperation.

Trailing 7-3 going into the ninth, Rob Refsnyder walked and Jacoby Ellsbury singled. Brett Gardner singled off Rangers closer Sam Dyson and a misplay in the outfield allowed Refsnyder to score. After Alex Rodriguez lined out to second, McCann slammed a three-run homer, his second of the game, to tie it at 7. Starlin Castro walked before Gregorius finished it with a homer to right.

“I think each guy each guy basically did his job and didn’t try to do too much,” Girardi said. “You feel that you’re in a pretty good spot when you’re down three and you’ve got some guys that can hit the ball out of the ballpark.”

It didn’t ever seem like it would get that far, especially after a forgettable performance from Masahiro Tanaka, one of the Yankees’ more reliable arms.

Tanaka struck out the first two batters he faced and appeared to stall after that, giving up a leadoff double to Prince Fielder in the second and a one-out RBI single to Elvis Andrus to start things off for the Rangers.

Although Chase Headley tied it up in the bottom of the inning on his fifth home run of the year — the 500th RBI of his career — the third inning proved to be disastrous continuation for Tanaka and the Yankees.

And, with this injury-riddled lineup, it wasn’t one they seemingly had much hope of overcoming.

The Rangers scored four runs in the third, keyed by back-to-back leadoff singles by Robinson Chirinos, the No. 9 hitter, and Shin-Soo Choo. Tanaka walked Ian Desmond to load the bases and took the next batter, rookie Nomar Mazara to a full count, before Mazara ripped an 87-mph slider to center for a bases-clearing double.

One batter later, Fielder hit a hopper to first base that glanced off the glove of a diving Rob Refsnyder to score Mazara for the 5-1 lead. Though that was deemed a hit, Refsnyder — relatively new at the position and in for Mark Teixeira, who was resting the sore right knee that landed him on the disabled list earlier this month — had made an error in the first inning.

Tanaka allowed six runs — all earned — on eight hits over eight innings, with a walk and seven strikeouts. Headley (1-for-3) had a sacrifice fly in the sixth.

Alex Rodriguez was 0-for-5.

Leadoff homers by Rougned Odor — in the sixth, off Tanaka — and Adrian Beltre — off Luis Cessa in the eighth — gave the Rangers a 7-2 cushion, and it appeared to be plenty of room. After all, this Yankees team seemed on the brink of being sellers come the trade deadline.

But, if the ninth inning is any indication, it might just be too early to count them out.

More Yankees headlines