SAN FRANCISCO (AP) Bruce Bochy's 800th victory as Giants manager was anything but typical.

Three blown leads, four hit batters, a rare shaky outing from Johnny Cueto, and back-to-back doubles in the ninth inning by two seldom-used players that provided the winning run.

However, with the third-best record in baseball, Bochy certainly isn't complaining.

More from FoxSports

Conor Gillaspie hit a game-ending RBI double with one out in the ninth inning to lift San Francisco past the Philadelphia Phillies 8-7 on Sunday and give Bochy his milestone win.

''It was appropriate I guess to have this type of game, a torturous game, where it looked like we had things under control and it started to get away from us,'' Bochy said. ''It's a good win, because that would have been a tough one to take, to lose this ballgame.''

Bochy became the fourth manager in franchise history to reach the 800-win mark, joining John McGraw (2,583), Dusty Baker (840) and Bill Terry (823). Bochy, who had 951 wins with San Diego, leads active managers with 1,751 - 16th all-time.

''I don't think John McGraw has anything to worry about,'' Bochy joked.

Ramiro Pena doubled down the right-field line off Severino Gonzalez (0-1) then scored standing up when Gillaspie lined a 1-1 pitch in the same direction. It was Gillaspie's second career walkoff hit and the seventh by the Giants this season, most in the majors.

Angel Pagan had a season-high four hits and two RBIs while five others had two hits apiece for San Francisco, which has won 13 of 15.

A day earlier the Giants let a late lead slip away with ace Madison Bumgarner on the mound.

''After the game yesterday . we had to come back again strong,'' Pagan said. ''We came back to play good baseball today.''

The teams combined for 28 hits, with the Phillies rallying to tie the score twice in the final three innings before Gillaspie's game-winner.

Odubel Herrera homered and Peter Bourjos added three hits for Philadelphia.

All three games in the series were decided by one run.

''San Francisco is a good team,'' Phillies manager Pete Mackanin said. ''We were a little bit of thorn in their side in this three-game series.''

Cory Gearrin (3-0) retired two batters for the win.

The Giants wasted leads of 5-1, 6-5 and 7-6 but held on for the wild win in a game that was highlighted by four hit batters - three by Philadelphia starter Aaron Nola.

Nola struggled with his command and failed to make it out of the fourth inning for the fourth consecutive start. He allowed five runs and 10 hits in just 3 1/3 innings with four strikeouts.

Giants starter Johnny Cueto wasn't much better.

The San Francisco right-hander gave up a season-high six runs in six-plus innings. The right-hander was attempting to become the first 12-game winner in the NL but wasted a four-run lead and left with no decision.

FINDING HIS STROKE

Pagan had his first four-hit game since April 25, 2015 at Colorado. The veteran outfielder, who has batted at six different spots in San Francisco's batting order, went 8 for 13 in the series against the Phillies. ''The reason I came over to the Giants is because of my versatility to be able to hit anywhere in the lineup,'' Pagan said. ''I always tell Bochy I'm always in my motorhome ready to contribute anywhere in the lineup.''

WARNING SIGNS

Both teams drew a warning from home plate umpire Doug Eddings with two outs in the fourth after Cueto hit Phillies cleanup hitter Maikel Franco with a pitch. That came after Nola hit three Giants, including two in the third. ''It was a pitch that just got away from me,'' Cueto said. ''It was a sinker that I was trying to throw. I don't understand why he gave me a warning when their pitcher hit our guys three times, also with a breaking ball.''

TRAINER'S ROOM

Phillies: After the game right-hander Vince Velasquez was activated from the disabled list. LHP Elvis Araujo was optioned to Triple-A to Lehigh Valley to make room.

Giants: Matt Cain will have one more throwing session this week and could be sent out on a rehab assignment as early as Friday. Cain has spent two stints on the disabled list due to a right hamstring strain. ... Buster Posey was hit on the outside of his left foot by a bunted foul ball in the fifth inning and had to be checked by head athletic trainer Dave Groeschner but remained in the game.

UP NEXT

Phillies: Velasquez (5-2) is scheduled to pitch the opener of a three-game series in Arizona on Monday. Velasquez had been out since June 9 with strained right biceps.

Giants: RHP Jeff Samardzija (8-4) pitches against Oakland on Monday after lasting just three innings in his most recent outing against Pittsburgh, which matched the shortest start of his career.