WASHINGTON — Gen. Mark A. Milley was never meant to be President Trump’s top military adviser.

Defense Secretary Jim Mattis had sent him to the White House in late 2018 to interview for the top American military post across the Atlantic, with its grand title: supreme allied commander Europe. Mr. Mattis wanted someone else, the quiet and cerebral Gen. David L. Goldfein of the Air Force, to be Mr. Trump’s next chairman of the Joint Chiefs of Staff.

With the president souring on Mr. Mattis, his recommendation quashed General Goldfein’s chances. During the meeting, the president — who already liked General Milley’s brash demeanor as Army chief of staff — asked which job was better. And General Milley went for the top prize: by law, the chairman of the Joint Chiefs of Staff is the nation’s top officer and the senior military adviser to the president.

But in the last several days, after accompanying the president from the White House to a church in his camouflage uniform as National Guard troops in helmets and riot gear deployed across the country, General Milley has quickly become the face of what could amount to the American military’s fall from public grace, to levels not seen since the Vietnam War.

“Milley (he’s a general !?!?) should not have walked over to the church with Trump,” Michael Hayden, the retired Air Force general who has directed both the National Security Agency and the C.I.A., said on Twitter, noting that he “was appalled to see him in his battle dress.”

General Milley’s decision to join Mr. Trump “was an egregious display of bad judgment, at best,” said Paul D. Eaton, a retired major general and veteran of the Iraq war, who now serves as a senior adviser at VoteVets.org. “At worst, Milley appears confused about the oath he took to support and defend the Constitution — not a president. I suggest the general get quickly unconfused, or resign.”

General Milley, his friends say, has agonized over the events of the past week. But he has also managed to persuade Mr. Trump not to invoke the 1807 Insurrection Act to deploy active-duty troops across the country to quell protests, a line that a number of American military officials say they will not cross, even if the president orders it.

It is largely because of General Milley, administration officials say, that Mr. Trump has not ordered it yet despite his threat to do so.

The general went face to face with his boss on Monday during a heated discussion in the Oval Office over whether to send troops into the streets, according to people in the room. He argued that the scattered fires and looting in some places were dwarfed by peaceful protests, and should be handled by the states, which command local law enforcement and the National Guard.

General Milley won the immediate battle in the Oval Office meeting on Monday. But shortly after, he was right in the middle of a different war — the kind of political war where the military does not belong.

Defense Department officials say General Milley believed that he was accompanying Mr. Trump and his entourage to review National Guard troops and other law enforcement personnel outside Lafayette Park; that he did not know the park had just been cleared of peaceful protests by security forces using tear gas; that an Australian news crew had just been beaten by baton-wielding police on live TV; that frightened teenagers were sobbing two blocks away.

Credit…Doug Mills/The New York Times

One Defense Department official on Friday likened General Milley’s walk across the park to walking through hell wearing gasoline underwear.

Whatever the case, the video, broadcast repeatedly, shows Mr. Trump, flanked by General Milley in the combat fatigues he wears every day to work, and a group of mostly white men crossing a park that had just been cleared of people protesting the killing of a black man after a white police officer knelt on his neck for close to nine minutes.

Once Mr. Trump arrived at St. John’s Church, holding a Bible, and it became clear that the moment was only a photo op, General Milley disappeared from view. He is nowhere to be seen when the president motions for other officials to join him for a photograph, in which he is now flanked by his press secretary, defense secretary, national security adviser and attorney general.

But the damage had been done.

“Ridiculous. General Milley, who I respect, is embarrassing himself,” Michael McFaul, a former American ambassador to Russia under President Barack Obama, tweeted.

Pentagon officials say General Milley was horrified afterward, and he has not appeared before cameras since.

Like all relationships the president has with his subordinates, his with General Milley is a tenuous one. There is a lot of “bro” talk and what passes for bonhomie between the two. General Milley, the son of a Marine who fought at Iwo Jima and who then tried to stop his own son from joining the military, mixes banter and bluntness when he talks to his boss, officials say.

But he is managing the most consequential professional relationship of his life with mixed results so far.

It is unclear if Mr. Trump has given more than a cursory look at his senior military adviser. General Milley has a hard exterior that neatly fits Mr. Trump’s idea of what a general should look like. But the general, a Princeton graduate with a penchant for long discourses on historical warfare, is probably as cerebral as General Goldfein, the Air Force chief the president rejected for the top military job.

Speaking to troops in Afghanistan in November, Mr. Trump expressed his usual suspicion of education, in comments about General Milley’s academic pedigree. “You know, he went to Princeton,” Mr. Trump said. “And he went to Columbia. I’m not sure — was that a good thing or a bad? I don’t know.”

Although Defense Secretary Mark T. Esper is the top official in the civilian-led Defense Department, Mr. Trump treats General Milley as the de facto head of the military. (He is not — he is the military’s most senior official; the services are led by their chiefs and secretaries, who all report to the defense secretary and the president.)

Last fall, Mr. Trump misspelled his defense secretary’s name in a tweet, referring to him as Mark Esperanto.

General Milley has told associates that when he deals one on one with Mr. Trump, the president listens and is attentive. But when the president is in a large group meeting, General Milley has told people that the atmosphere can be more tense and the performer in Mr. Trump emerges.

General Milley, who turns 62 this month, has a cordial relationship with Mr. Esper, 56, but officials who have attended meetings with the two say General Milley, a four-star general who has earned both Ranger and Army Special Forces tabs and commanded troops in Iraq and Afghanistan, sometimes treats Mr. Esper like the junior Army officer he once was.

On Monday, after Mr. Trump said General Milley was “in charge” of the protest response, Representative Ruben Gallego, Democrat of Arizona and a former enlisted Marine who saw heavy combat in Iraq, sent General Milley a one-line letter asking, “Do you intend to obey illegal orders from the President?”

In the tumultuous hours and days since the walk across the park, General Milley has taken pains to mitigate the damage.

It actually was General Goldfein of the Air Force who was the first of any senior Pentagon leader to break an Esper-imposed silence and comment on the extraordinary events around the killing of Mr. Floyd.

On Wednesday, General Milley released his own letter that forcefully reminded the troops that their military is supposed to protect the right to freedom of speech. He added a handwritten codicil to his letter, some of it straying outside the margins: “We all committed our lives to the idea that is America — We will stay true to that oath and the American people.”

While that letter came more than a week after Mr. Floyd was killed, “It’s a start,” James Stavridis, a retired admiral and the former supreme allied commander Europe, said in an email. “All of the service chiefs have also put out guidance against racial discrimination. I think it’s about as far as they can go in uniform without resignations.”

David Lapan, a former spokesman for the Department of Homeland Security in the Trump administration, called General Milley’s letter pro forma.

“He could have said that a week ago,” he said.

In the hours after the photo op, General Milley, still in his combat fatigues, walked the streets of downtown Washington, talking to National Guard troops and protesters. He stayed out until after midnight.

Michael Crowley contributed reporting.