SAN DIEGO (AP) -- Madison Bumgarner struggled in the first inning, allowing two runs on three hits.
Then he retired 15 straight batters, keeping the San Diego Padres off-balance and giving his San Francisco Giants a chance to catch up.
Bumgarner, a first-time All-Star, allowed just four hits in seven innings, and Pablo Sandoval hit his first home run since May 21 as the Giants beat the Padres 4-2 Thursday night in a matchup of the worst teams in the weak NL West.
It was the third straight start in which Bumgarner snapped a four-game losing streak.
''That's why he's on the All-Star team, too,'' said manager Bruce Bochy, who will lead the NL team on Tuesday night. ''We needed a lift. Inauspicious start there, but that's what good pitchers do. They limit the damage. He got out of that. What was impressive about Madison he was flat the first inning until he made a nice adjustment and got in a great groove there and pitched great.
''Early, he was getting under the ball and leaving pitches up. But he made a nice adjustment and gave us seven solid innings in a much, much-needed win.''
Bumgarner (10-5) struck out six and walked one.
Bumgarner didn't think he made much of an adjustment.
''I felt I made some pretty good pitches there in the first and they just happened to hit them,'' he said. ''I just kept making pitches and throwing strikes, getting ahead. They still hit some balls hard. Just happened to be right at the defenders.''
Sergio Romo pitched the ninth for his 21st save in 24 chances.
Romo agreed with Bochy about how much the Giants needed the performance from Bumgarner.
''It's one of those things where we've been spoiled in the past with the caliber of starting pitching,'' Romo said. ''To be able to do that tonight is big for us. We really, really needed it.''
The defending World Series champion Giants, who snapped a four-game losing streak, have been in a free-fall since June 15. They had lost 19 of 24 games coming in and were a season-high 10 games under .500. They started the night two percentage points behind the Padres and ended it by jumping past San Diego into fourth place.
The Giants are 2-8 in July, with Bumgarner getting both wins.
Everth Cabrera, San Diego's only All-Star, said Bumgarner started throwing his backdoor cutter better after the first.
''He's got such good pitches and he makes you think about his cutter and he fools you with something else,'' Cabrera said. ''He's really tough, probably as tough of a pitcher that we face.''
The Padres have lost 16 of 19.
Sandoval got San Francisco's weak offense going in the fourth with a leadoff homer to right-center off Jason Marquis, his ninth.
It was Sandoval's first homer in 101 at-bats. Sandoval, who was on the disabled list from June 9-24 with a strained left foot, was 6 of 55 (.109) before that at-bat.
''I thought I executed the way I wanted to, but what can you say?'' Marquis said. ''The guy, over the years, has hit some pitches that shouldn't be hit.''
The Giants had the bases loaded three times in the sixth and seventh innings but scored only one run.
Marquis took a 2-1 lead into the sixth and struck out Buster Posey leading off the inning before loading the bases and allowing Kensuke Tanaka's RBI single up the middle. Marquis made way for Joe Thatcher, who got Brandon Crawford to hit into a force at the plate. Dale Thayer came on and struck out Bumgarner to end the inning.
The Giants loaded the bases again in the seventh on two singles and a hit batsman, and Thayer again got out of trouble by striking out Belt to end it.
San Francisco finally broke through in the eighth with two runs and three hits off Luke Gregerson (4-5), including an RBI double by Blanco that bounced over the fence in left, and Posey's RBI single to left.
Bumgarner settled down after a rough first inning in which he allowed two runs on three hits. Carlos Quentin hit an RBI double and, after Jesus Guzman was intentionally walked to load the bases, Kyle Blanks drove in a run on a fielder's choice. Bumgarner got Logan Forsythe to line out to second to end the inning.
Marquis failed for the fifth time to get his 10th victory. He allowed two runs and seven hits in 5 1-3 innings, struck out three and walked two.
NOTES: The Giants have won 10 of Bumgarner's last 11 starts against the Padres. ... The four-game series continues Friday night when right-hander Sean O'Sullivan makes his debut for his hometown Padres against San Francisco RHP Chad Gaudin (2-1, 2.44 ERA). O'Sullivan, who graduated from Valhalla High in suburban El Cajon in 2005, was signed by the Padres as a minor league free agent in December. It will be his first big league start since Sept. 27, 2011, with the Royals. ... Padres 1B Yonder Alonso is expected to be in the starting lineup for Friday's game He has been on the disabled list since May 31 with a broken right hand.