“I think that we can expect more of this in the future now that the line’s been crossed,” said Peter R. Mansoor, a retired Army colonel and a former aide to Mr. Petraeus, who is now a professor at Ohio State University. “I’m not sure it’s healthy for civil-military relations.”

The risk, Colonel Mansoor said, is that “whether the criticism is deserved or not, future presidents may be less willing to trust their senior officers, thinking they’ll turn on them once they’re out of uniform, or they may be less willing to promote officers who are very competent but perhaps independently minded.”

While Mr. Mattis’s resignation was the first over a major national security issue by a leading cabinet member since 1980, when Cyrus Vance quit as secretary of state, his conflict with Mr. Trump was hardly the first time that there have been clashes between civilian and military officials.

In 2006, a number of retired military officers publicly rebuked Defense Secretary Donald H. Rumsfeld for his handling of the Iraq war. In 1993, President Bill Clinton was at odds with military officials over allowing gay men and lesbians in the military. And there was a hefty amount of distrust between President Barack Obama and military officials over the administration’s approach to Iraq and Afghanistan.

During the 2016 presidential campaign, Michael T. Flynn, a retired Army lieutenant general, led chants of “Lock her up!” about Hillary Clinton at the Republican National Convention, while John R. Allen, a retired Marine general, as a speaker at the Democratic National Convention, questioned Mr. Trump’s ability to be the commander in chief.

But Peter Feaver, a former National Security Council official under Mr. Clinton and President George W. Bush who now teaches political science at Duke University, said Mr. Trump has taken these clashes to a new level.

“We’ve never had a president who was as insensitive to the norms and taboos” of the military, Mr. Feaver said. “People like McChrystal, people like McRaven and the others are responding to the president’s breaking of these taboos.”

“No American president has ever dared risk the American civil-military relationship for less cause or with such childish malice,” Tom Nichols, a professor at the Naval War College, wrote in The Atlantic after the McChrystal tweets.