AP

SAN FRANCISCO (AP) — San Francisco Giants manager Bruce Bochy was out of options after emptying his bench of position players three innings earlier and with the potential winning run at third base in the 12th inning. Then, he turned to his best hitting pitcher.

It worked perfectly.

Madison Bumgarner had the first game-ending hit of his career, a pinch-hit single that drove in Gorkys Hernandez, and lifted the Giants to a 5-4 victory over the San Diego Padres on Tuesday night.

The Giants recorded their 10th walk-off win of the season, and won for just the fifth time in 22 games.

"It's tough this time of year to come and compete and play as hard as you ordinarily would but this is the time you have to look yourself in the mirror and go out there and do your job, earn your paycheck," Bumgarner said "As soon as we ran out of guys, I got my cleats and my jersey on."

San Francisco blew a 4-3 lead in the ninth when Wil Myers doubled and scored off Tony Watson. But the Giants rallied in the 12th after going hitless the previous four innings.

After Hernandez tripled off Rowan Wick (0-1), Bochy called for Bumgarner.

Bumgarner took a called strike and then lined a sharp single past third baseman Myers to drive in the winning run. It was Bumgarner's fourth career pinch-hit and first since July 31, 2016. He has six hits and three RBIs in 41 at-bats for a .146 average. He has a career batting average of .182.

"He smoked it, didn't he?" Bochy said.

Mark Melancon (1-3) retired six batters to earn win.

Hunter Pence homered and scored three times in what was likely one of his final games with San Francisco. Gregor Blanco had a two-run single and Evan Longoria added an RBI double.

Freddy Galvis had four hits and a walk, continuing his offensive surge for San Diego. Eric Hosmer added a two-run single.

At 63-95, the Padres are assured of their worst record since 2008, when they finished 63-99.

San Diego stranded a runner at third in the 11th and at first and third in the 12th. The Padres also matched their season high of three errors, two by shortstop Javy Guerra.

"It's a tough one for us," San Diego manager Andy Green said. "We gave away some runs on the defensive side and it's tough. We've played relatively clean defense for a while and we didn't really do that today."

Pence, who reached on an error and scored in the first, homered off Robbie Erlin in the fourth and received a standing ovation from the announced crowd of 36,063.

The 35-year-old outfielder also singled and scored the go-ahead run on Gregor Blanco's double off Robert Stock (1-2) in the seventh. Blanco had been 5-for-32 with runners in scoring position.

Recognized as one of the driving forces behind the Giants' World Series titles in 2012 and 2014, Pence is scheduled to become a free agent in the offseason.

TRAINER'S ROOM

Giants: Panik didn't start but entered as a pinch-hitter in the seventh. Catcher Nick Hundley was hit in the back of the head on a follow-through swing by Austin Hedges in the 12th inning, but remained in the game.

UP NEXT

San Diego RHP Luis Perdomo (1-6, 7.56) makes his first start since July 27 in the series finale Wednesday as part of manager Andy Green's plan to bullpen the game. Perdomo has made two relief appearances after being recalled from the minors earlier this month. RHP Casey Kelly (0-2, 2.04) makes his third start for the Giants and first against his former team. Kelly pitched for the Padres in 2012 and 2015.

__

More AP MLB: https://apnews.com/tag/MLB and https://twitter.com/AP_Sports