The Giants scored three runs in the third inning, thanks to RBI singles from center fielder Angel Pagan, third baseman Pablo Sandoval and catcher Buster Posey. The Cubs responded with a run of their own the same inning, and took the lead with four runs in the fourth.
San Francisco tied things up after a triple by pinch hitter Brandon Belt, who later scored on a wild pitch. In the ninth inning, the Giants put up two more runs, enough to secure the victory.
Starting pitcher Matt Cain was given a no decision after allowing five runs in five innings.
The win improves the Giants to 76-58 on the season while the Cubs fell to 51-82. San Francisco will next take on the Arizona Diamondbacks beginning on Monday.