PHOENIX (AP) Giants manager Bruce Bochy has confidence in Kelby Tomlinson, even though the 25-year-old infielder was recalled from Triple-A Sacramento only last week.
Tomlinson made Bochy look good late Friday night when his pinch-hit single with one out in the 12th inning drove in the go-ahead run and helped San Francisco outlast the Arizona Diamondbacks for a 7-6 victory.
''He's got a base-hit swing. He's short to the ball. He's not a strikeout guy,'' Bochy said. ''He does a good job of putting it in play. He's a guy you want up there. He makes hard contact consistently.''
Brandon Belt drew a leadoff walk from Dominic Leone (0-1) in the 12th before Joe Panik singled.
San Francisco maintained its half-game lead over the Mets for the top NL wild card, with St. Louis just a half-game behind New York.
''Any way to help the team win is great, and to do it with the bat just kind of adds a little bit more versatility, I guess,'' Tomlinson said.
In an unorthodox move, Giants reliever Cory Gearrin got one out before being moved to left field for one batter so lefty Javier Lopez could face left-handed hitter Jake Lamb, who tied the score at 6 with a pinch-hit homer in the 10th.
Bochy then put Gearrin back on the mound to finish the game for his third save. With two runners on, Gearrin struck out Welington Castillo looking for the second out on a 3-2 pitch, and Castillo was ejected for arguing.
Yasmany Tomas grounded out to end it.
Joe Nathan (2-0) escaped an 11th-inning jam in a game that lasted 5 hours, 23 minutes.
''They are playing for their lives over there. They know it,'' Diamondbacks manager Chip Hale said of the Giants. ''I thought our guys matched it. It just didn't come out our way.''
Arizona used 26 players, including 12 pitchers - both franchise records.
Gearrin became the first Giants pitcher to play the field since 2007.
San Francisco moved within four games of the first-place Los Angeles Dodgers in the NL West.
''Exciting game. A little too exciting. Hard-fought,'' Bochy said. ''Huge win, obviously, to come back like that and lose the lead a couple of times and end up winning the game.''
Hunter Pence gave the Giants a 6-5 lead in the top of the 10th, scoring on Enrique Burgos' wild pitch. Pence had four hits and made a key defensive play in right field.
The Giants turned to Santiago Casilla to try to close it out, but Lamb extended the game with a solo homer in the bottom of the 10th.
The Diamondbacks made it 5-all in the seventh when Giants center fielder Denard Span dropped Chris Owings' long fly for a three-base error. Pinch-runner Socrates Brito scored on the play, but Owings was thrown out trying to score.
That was after Angel Pagan's solo home run gave the Giants a 5-4 lead.
Pence took an extra-base hit away from Mitch Haniger in the bottom of the ninth with a sliding catch in the right-field corner.
Giants ace Madison Bumgarner was briefly in line for his 100th career win, but ended up tossing six innings and allowing four runs on six hits with nine strikeouts and a walk in a no-decision.
The Diamondbacks built a 4-1 lead against Bumgarner with two-run homers by Brandon Drury in the second inning and Kyle Jensen in the third, Jensen's first major league home run.
Jensen, called up from Triple-A Reno on Sept. 3, entered as an injury replacement for A.J. Pollock in the top of the second.
''Drury hit a decent pitch and just too many of the same thing to the other kid. I have to give those guys more credit than that,'' Bumgarner said.
Diamondbacks starter Rubby De La Rosa was on a pitch limit in his first start since late May. He was activated from the 60-day disabled list before the game after coming back from a right elbow injury and threw 43 pitches before leaving for a pinch hitter.
De La Rosa struck out three in two innings but gave up a run in the second on an infield single, two walks and Eduardo Nunez's sacrifice fly.
Giants: RHP Derek Law is on the 15-day disabled list with an elbow injury. He could be activated by early next week. ... OF Gregor Blanco is on the 15-day DL with a right shoulder injury. He's hitting off a tee while working out in Arizona. ... Nunez was slow running up the line on a sixth-inning single, but after being checked by a trainer he stayed in the game.
Diamondbacks: Pollock left in the bottom of the first after appearing to get injured running out a fly ball. Pollock only recently returned from his season-long rehabilitation of a broken elbow. Hale had no update on Pollock's status after the game. ... Backup catcher Chris Herrmann broke his left hand while sliding, Hale said. ... With De La Rosa activated, infielder Nick Ahmed (hip) was transferred to the 60-day disabled list.
Giants: RHP Johnny Cueto (14-5) faces the Diamondbacks on Saturday for the fifth time this season. He is 2-2 in the previous four starts.
Diamondbacks: RHP Archie Bradley (6-8) has two wins in his last three starts.