A man accused of slashing another man with a sword after having his “Make America Great Again” hat knocked off his head last weekend in San Francisco appeared in court Friday to face charges in the attack.

Leor Bergland, 30, was defiant as he stood in his orange jail-issued clothing for an initial appearance in San Francisco Superior Court. He was arrested Wednesday and charged with attempted murder, mayhem and assault with a deadly weapon.

“You just basically implied that I did it,” Bergland said as prosecutors argued for him to be held in jail without bail.

“Why are you telling me to be quiet?” Bergland then said to his attorney, Deputy Public Defender Eric Quandt. “I can defend myself.”

Bergland, whose messy brown hair covered his eyes, did not enter a plea during the brief appearance in which Judge Donna Little set his bail at $250,000.

The attack took place around 9:45 p.m. on March 29 outside a roller rink at 554 Fillmore St., prosecutors said. The victim, 27-year-old Gabriel Gaucin, passed under construction scaffolding near the scene and saw a man walking toward him in a red flannel shirt and red “Make America Great Again” hat that was popularized by President Trump during his 2016 run for president.

The victim swatted the hat off Bergland’s head, prosecutors said. That’s when Bergland “suddenly reached back, as if to punch the victim, but instead quickly brought his hand down, holding what the victim thought was an umbrella or nightstick,” prosecutors wrote in court papers filed Friday.

Gaucin’s hand was partially severed in what prosecutors in court Friday described as “one of the most gruesome photos of an assault I’ve ever seen.” He was treated at San Francisco General Hospital and had emergency hand surgery.

A witness at the scene chased the assailant, who disappeared into nearby Alamo Square Park, officials said. Police seized the red hat and a beer bottle the suspect had been seen drinking at the scene.

Two days later, a security guard found a sword wrapped in a red plaid vest protruding from some garbage cans at 1175 Pierce St. and called 911, prosecutors said.

Police matched a fingerprint on the beer bottle to Bergland and arrested him Wednesday inside a restaurant on Eddy Street, officials said. The defendant told police he was home at the time of the incident and stated, “What do you have on me, nothing ’cause I didn’t do it,” prosecutors said.

“No one has a right to strike someone with a sword because their hat was knocked off their head,” said Alex Bastian, a spokesman for the district attorney’s office. “Those are the allegations in this case and those things will be figured out in court.”

Quandt argued that the case calls for restorative justice and that both sides should sit down together and try to work it out. His client, he said, didn’t instigate the altercation.

“What the reports seem to suggest is that this defendant reacted to being either attacked, or a hat thrown off his head, or something along those lines,” Quandt said. “We will find out much more in the coming weeks.”

Evan Sernoffsky is a San Francisco Chronicle staff writer. Email: esernoffsky@sfchronicle.com Twitter: @EvanSernoffsky