Cincinnati Reds right-hander Homer Bailey couldn't help but think about the possibility of throwing his third career no-hitter Sunday as he racked up out after out against the San Francisco Giants, a team he no-hit last year.

"I wasn't really thinking about it other than the obnoxious fan above our dugout who kept yelling at me about it," Bailey said. "He just kept talking about, 'You haven't given up a hit.' It's like, 'I know.'"

Bailey had a no-hitter for 6 2/3 innings but settled for his fourth career shutout as the Reds completed a four-game sweep of the San Francisco Giants with a 4-0 victory at AT&T Park.

Bailey (8-4) allowed just three hits, struck out seven and walked one, pitching his sixth career complete game.

The four-game sweep was Cincinnati's first against the Giants since April 18-21, 1985 at Riverfront Stadium and their first at San Francisco since May 16-18, 1972. The Reds' last four-game sweep against any opponent was against the Atlanta Braves on May 21-24, 2012. The Giants had never been swept in a four-game series at AT&T before Sunday.

Cincinnati won its season-high fifth straight game and prevailed for the 14th time in its past 18 games. The Reds moved into a tie for second place with St. Louis in the National League West, 6 1/2 games behind Milwaukee, and won their fifth straight series.

"As you can see by the way we're playing, the way we've been playing the last five or six series, we're definitely not out of it," Bailey said. "So there's a lot of hope in the way that we're playing very well. Just slowly trying to gain ground."

Bailey had a no-hitter until catcher Buster Posey hit a solid single to right field with two outs in the seventh, drilling a 1-1 fastball. Third baseman Pablo Sandoval blooped a single to left-center field, moving Posey to third, but Bailey struck out left fielder Michael Morse on three pitches, including a 97 mph fastball that Morse took for strike three.

"His stuff is really good right now," Reds left fielder Skip Schumaker said. "He's done it before. I thought yeah, he might have a shot. But getting Posey and (Hunter) Pence out four times is not an easy feat. I thought if he got those two guys out we'd have a chance, but Buster pulled though."

Bailey pitched his second career no-hitter on July 2, 2013, against the Giants, striking out nine and walking one in a 3-0 Reds victory at Cincinnati. He also no-hit the Pirates on Sept. 28, 2012 at Pittsburgh, fanning 10 and walking one.

Giants right-hander Tim Hudson (7-4) allowed just two runs on five hits over eight innings but took his third consecutive loss. Hudson struck out six, walked none and threw 106 pitches, 75 for strikes.

The Giants lost for the 15th time in their past 19 games since June 9. They have the worst record in baseball during that span. They owned a 9 1/2-game lead in NL West on June 8 but have now blown it all and are tied with the Dodgers for first place.

"There's disappointment but guys are still going out and doing the best they can," Hudson said. "We haven't played the way we did the first month of the season. It was a tough game for us. We just have to keep grinding it out. We have to go out as a staff and give ourselves a chance to win."

The Reds took a 1-0 lead into the top of the ninth before adding three insurance runs. Third baseman Todd Frazier lined a leadoff single to left, ending Hudson's day. First baseman Joey Votto moved Frazier to third with a single to left off reliever Jeremy Affeldt. Second baseman Brandon Phillips brought Frazier home with a sacrifice fly to right.

After Affeldt beaned right fielder Jay Bruce, catcher Brayan Pena grounded an RBI single down the first base line, scoring Votto. Schumaker made it 4-0 with a run-scoring infield single.

The Reds took a 1-0 lead in the top of the third. Leading off, Schumaker grounded a sharp single to center field, Cincinnati's first hit off Hudson. Schumaker moved to second on Bailey's sacrifice bunt and scored on speedy center fielder Billy Hamilton's double to shallow right-center.

Bailey didn't allow a baserunner until center fielder Gregor Blanco walked while leading off the fourth. Blanco advanced to second on Pence's groundout. With Posey facing an 0-2 count, Blanco took off for third before Bailey went into his delivery. Bailey stepped off the rubber and threw to Frazier, easily nailing Blanco. Bailey struck out Posey, ending the inning.

"We ran into four starters as dominant as any four starters I've ever seen," Giants manager Bruce Bochy said of Bailey, Johnny Cueto, Mike Leake and Alfredo Simon. "We have to be thankful to a good start. As bad as things are, we have to consider ourselves lucky to be where we are."

NOTES: RHP Sergio Romo lost his job Sunday as San Francisco's closer, and the Giants will use a closer by committee for the foreseeable future, manager Bruce Bochy said. Romo blew his third save in his past five chances Saturday night in a 7-3, 11-inning loss to Cincinnati. RHP Santiago Casilla and LHP Jeremy Affeldt will be Bochy's top options in the ninth inning. "Sometimes you got to change things," Bochy said before Sunday's game. "If you keep doing what you're doing and keep getting what you're getting. Sometimes it's good for the player to back him off a little bit, let him catch his breath. Let's get him back on track." ... Reds CF Billy Hamilton entered Sunday's game leading all National League rookies in hitting, RBIs, multi-hit games, runs, hits, total bases, stolen bases and on-base percentage.