SAN FRANCISCO (AP) -- As awful as the San Francisco Giants have played for most of the last month, they are back in an enviable position: first place in the NL West.
Beating their Bay Area rivals to get back there was a nice bonus, too.
San Francisco, which had lost 20 of 27, moved percentage points ahead of the Dodgers in the division.
''If you look at how things have gone, you find that amazing,'' Giants manager Bruce Bochy said. ''It's all from the good work that happened earlier. We've got to get back on track.''
For one night, they did.
Cain (2-7) allowed two runs and five hits for his first win since May 15 against Miami. He struck out four and walked two to help the Giants snap major league-leading Oakland's six-game winning streak.
''Felt really good,'' Cain said. ''Guys came up with some big, clutch hits when we needed them. Guys kept putting pressure on those guys, which is always nice.''
Santiago Casilla got three quick outs for his fourth save.
Hammel, acquired by the A's along with fellow right-hander Jeff Samardzija from the Chicago Cubs last week for three top prospects, had anything but a clean opener with Oakland.
He pitched with runners on base for most of the night, and his left thumb on his glove hand cramped up and looked disjointed at one point. He left after giving up six hits and three runs - two earned - in five innings, striking out three and walking three.
Hammel said his thumb was fine afterward and he was bothered more by his performance.
''Tonight was a grind and they grinded me pretty good,'' he said.
Stephen Vogt homered and Jed Lowrie had an RBI single for Oakland.
The A's shut down the Giants 5-0 and 6-1 in the first two games of the Bay Bridge Series in Oakland and looked every bit like a team with the best record in baseball while doing it.
Back in the comforts of glitzy AT&T Park, though, the Giants offense finally showed signs of life. San Francisco finished with 10 hits and took advantage of most of its scoring opportunities.
With one out in the second inning, Yoenis Cespedes bobbled Joe Panik's single in left field for an error. By the time the strong-armed Cespedes corralled the ball and threw home, Gregor Blanco had crossed the plate.
Buster Posey's RBI single gave the Giants a 2-0 lead in the third.
Vogt homered leading off the fourth, sending a slider from Cain over the stands in right and onto the walkway outside the ballpark for his third long ball this season.
Pence came back with his own long ball in the fourth to put the Giants up 3-1. It was Pence's 12th home run and first since June 21 at Arizona.
''It was a pretty clean game,'' Posey said.
While the Giants finally got the bats going, Cain kept finding ways to minimize the A's rallies.
The right-hander got Coco Crisp to ground out with two runners on in the fifth, which came after he misfired to second base on a potential double-play ball earlier in the inning. And after Lowrie's two-out single sliced San Francisco's lead to 3-2 in the sixth, Cain got Derek Norris to ground out, stranding two runners.
Oakland got a brief scare in the fifth when Hammel's left thumb somehow bent awkwardly in the middle of Blanco's at-bat. An A's trainer worked on the thumb for several minutes, then the pitcher got Blanco to ground out.
Pence hit an RBI single in the sixth before lefty Eric O'Flaherty's wild pitch helped the Giants score another run to go ahead 5-2.
Oakland's offense was kept quiet the rest of the way. Jeremy Affeldt, Sergio Romo and Casilla each pitched one perfect inning of relief.
NOTES: The Giants optioned OF Juan Perez to Triple-A Fresno after the game. INF Ehire Adrianza will be activated from the disabled list Thursday. ... Giants 2B Marco Scutaro, who has been out all season with a strained lower back, is scheduled to play nine innings for Fresno on Thursday before rejoining San Francisco on Friday or Saturday. ... Vogt extended his hitting streak to a career-high nine games. ... Left-hander Scott Kazmir (10-3, 2.53 ERA) starts for the A's against RHP Tim Hudson (7-5, 2.53 ERA) in Thursday's series finale.