The leak was capped by 3:30 p.m. but the streets remained closed, San Francisco police Sgt. Troy Dangerfield said.
A private contractor hit a high-pressure gas line with a backhoe while working at 18th and Castro streets at about 1 p.m., San Francisco fire spokeswoman Mindy Talmadge said.
Officials believe the line that was hit is a 2-inch service line, but won't for sure until PG&E crews assess the situation, Talmadge said.
The smell of gas was reported in the 500 block of Castro Street just after 1 p.m., and pedestrians and motorists were told to move away from that area, police spokeswoman Lt. Lyn Tomioka said.
Buildings in the area were evacuated.
Tomioka said 18th Street is closed between Castro and Collingwood streets, and the 400 and 500 blocks of Castro Street have been shut down.
The wind is moving the smell of gas north, and there have been no reports of fire, according to Alert SF, a citywide e-mail alert system.
Some San Francisco Municipal Transportation Agency bus routes have been affected by the leak, including the 24-Divisadero, 33-Stanyan, 35-Eureka, and 37-Corbett.
Inbound and outbound Muni Metro service was bypassing the Castro station after the leak was reported, but service at the station was restored at about 3:25 p.m.
No injuries have been reported.