Two men in California were charged with hate crimes on Friday in connection with an attack on a Sikh man, punching him in the face and using a knife to cut up to 10 inches of his hair, which was unshorn by religious mandate, a prosecutor said.

The attack, which unfolded over the span of about three minutes and a half-mile stretch of road in Richmond, Calif., on Sept. 25, started by chance, officials said.

Five men who were doing subcontracting work at a refinery were staying at a local hotel and had been drinking beer most of the day when they went to get something to eat, Simon O’Connell, a deputy district attorney with the Contra Costa District Attorney’s Office, said in an interview on Friday.

The men were in a pickup around 8:45 p.m. when they pulled up to a red traffic light next to a sedan driven by Maan S. Khalsa, 41, of Richmond. In what Mr. O’Connell said was an unprovoked act, the occupants threw beer cans at Mr. Khalsa’s car. When he rolled down his window and said, “You guys forgot something,” the men, angered by his remark, followed him to the next red light.