SAN FRANCISCO (AP) The way things are going for the Pittsburgh Pirates these days, even when they dig themselves into a hole there's still hope.
''You start winning a few games, regardless of what the score is, if it's early we know we've got a chance to make something happen,'' Pirates center fielder Andrew McCutchen said.
The Pirates made something happen against the San Francisco Giants on Wednesday.
McCutchen had two hits, including a two-run homer, pinch-hitter Matt Joyce had a key two-run single, and Pittsburgh rallied from a four-run deficit to complete a three-game sweep of the Giants with a 6-5 victory.
''That's us,'' McCutchen added. ''That's what we do.''
The Pirates won their fourth straight game and sixth in seven. They remain a game behind St. Louis, which beat Houston 8-2, in the race for the NL wild card.
''I think this last week or two has really been fun for us,'' Joyce said. ''You can definitely tell guys are playing with that intensity and you can tell they're hungry.''
McCutchen's homer, his 17th, capped a six-run rally in the fifth inning that chased Giants starter Matt Cain (4-8).
Cain had allowed one hit and had faced just one batter over the minimum with a 4-0 lead going into his disastrous fifth inning, when the Pirates scored six runs on four walks, three hits and a hit batsman.
His first pitch to McCutchen was an 89 mph fastball over the middle of the plate, and the Pirates center fielder pounced.
Pirates starter Ivan Nova allowed four runs and six hits in four innings. He struck out five and allowed no walks. Nova, who won his first two starts since coming over in a deadline trade with the New York Yankees, was lifted for Joyce after Cain hit David Freese to start the fifth and then walked three straight batters.
Pirates manager Clint Hurdle said of his decision to pinch-hit Joyce wasn't a reflection on Nova's performance.
''How do you know you're going to have another shot with the bases loaded?'' Hurdle asked. ''You take a shot when the bases are full and see what can play out.''
Nova is 2-0 with a 4.41 ERA in three starts with the Pirates. He hasn't allowed a walk in 16 1/3 innings with his new team.
'' We like his stuff,'' Hurdle said. ''It was a tough day to be a National League pitcher. In the American League he's probably still pitching.''
Jeff Locke (9-7) pitched a scoreless fifth.
Tony Watson survived a scoreless ninth after the Giants loaded the bases with no outs for his seventh save in eight tries since taking over Pittsburgh's closer's role.
The Giants, who on Tuesday fell out of first place in the NL West for the first time in 97 days, lost their fourth straight. They fell to 9-21, the worst record in the majors since the All-Star break. They dropped 1 1/2 games behind the Dodgers, who beat Philadelphia on Wednesday night.
''We're taking some blows right now but it's a tough club, it's a resilient club,'' Giants manager Bruce Bochy said.
''They came out and did some good things today. We're creating some good chances and opportunities.''
The Giants failed to capitalize when it counted most.
After inducing Buster Posey to ground into a run-scoring double play, Watson got Brandon Crawford to fly out to shallow center and McCutchen made a game-ending running catch.
Posey was 3 for 5 with an RBI double to lead the Giants. Crawford was 2 for 5 with a double, a triple and three RBIs and Angel Pagan was 3 for 4 with a double as he extended his hitting streak to 15 games.
The Pirates are 25-8 on the road against the NL West over the last two seasons and are 22-11 against the division this season. ''This has been key for us the last couple of years, coming out here and taking care of business,'' McCutchen said.
Pirates: RHP Tyler Glasnow, on the DL since July 24 with right shoulder discomfort, threw a bullpen session on Tuesday and is showing progress, Hurdle said. ... C Chris Stewart, out since July 2 with knee discomfort, had his third scheduled rehab game with Double-A Altoona rained out on Tuesday.
Giants: Bochy had Posey in the lineup for a day game after a night game after the All-Star catcher missed two consecutive games with lower back tightness. ''I don't want him going out there if there's any risk of making it worse, he says he's fine,'' Bochy said. ... 1B Brandon Belt dropped to the ground after fouling a pitch off his right knee in the fifth inning but stayed in the game.
Pirates: RHP Gerrit Cole will pitch the series opener against the Marlins on Friday. Cole is 1-2 with a 6.00 ERA in three starts since pitching his first career complete game against Seattle on July 27.
Giants: LHP Madison Bumgarner will pitch the series opener with the Mets on Thursday. The All-Star threw seven shutout innings against Baltimore in his most recent outing to snap a streak of five winless starts in which he was 0-3 and the Giants were 0-5.