To be resistant to coup, distribute the power and leadership of both your country and our military so that it does not resemble the traditional, authoritarian, pyramidal structure.
We (humans) tend toward that structure, with ultimately one guy in charge: The CEO, the President, the Chief, the General. They command multiple units below them: Just those, the General and his Colonels, or the President and his Cabinet, or the CEO and his Vice Presidents, form a shallow pyramid, and that same kind of shallow pyramid is repeated at the next level, all the way down to the line manager in charge of a crew of workers, or a sergeant in charge of a group of soldiers.
This is a simple arrangement with a clear "chain of command", but it is a fragile arrangement: Change one level of command and everything beneath it in the chain of command is drastically affected. That is the vulnerability of coup.
For example, in one of my early jobs I worked for a hospital, at a rather low level. Two years before I began, they had put in a new CEO. The previous CEO had retired at age 70, and been replaced by an older medical doctor. Without any criticism whatsoever of his medical knowledge, he turned out to be a woefully incompetent CEO. So much so that after five years at the helm, the hospital got into major financial trouble and severe legal trouble, and was nearly closed by the Joint Commission (a regulatory body overseeing hospitals in the USA). In fact, thirty years later, this same hospital has never recovered from that five years of really just one person being changed at the top of the pyramid.
A coup is similar: Replace the top of the pyramid, whether it is the single top person, or the top 2 or 3 layers of command (a few dozen people), and the coup controls hundreds of thousands, because everybody beneath that have spent their lives accustomed to taking orders from "above", making any appeals they may have "up the chain of command", and so on.
To be coup resistant, you need a different structure of command, so there is no weak point that is easy to take over: Either in the military (for commanding a coup) or in the civilian government (for being vulnerable to a violent takeover).
There are various ways of doing this, but they all require eliminating thoughts to which we are accustomed, like the idea of "somebody in charge", or "commands."
For an example, take the structure of Gore, a company with 3.2B in sales and over 10,000 employees. They employ an "inverted management" structure: Bottom level working crews of up to around two dozen select their own leader, and can replace them at any time with another leader (bringing the original back down). That occurs all the way up the chain of command, the CEO is selected by the group s/he commands, and the group s/he commands can vote another from their group into the CEO position at any time. Nobody at Gore has to do any job they don't want to do, no "orders" are allowed or given. You cannot work in a group if no group votes to accept you, or if your group votes to eject you. It is democratic management (with various caveats, of course, for remaining within the hundreds of laws on workplace).
You might not think this could work, that nobody wants to clean the toilets, or risk their life: But when a job is distasteful, disgusting, or dangerous, enough compensation and respect for those that do it will elicit volunteers: Gore is still functioning after decades despite strictly adhering to this rule that there is no such thing as involuntary service. It is not a "carrot and stick" arrangement, there are only carrots: Offer enough carrots and somebody will tilt and take the job, for the money, or perks, or time off, or other freedoms that may come with it.
A similar dynamic exists at Whole Foods: In a study I read (more than ten years ago) Janitorial staff there earned three times the national average for their job, because they were paid by relative productivity, meaning how much other companies have to pay to get all their janitorial work done. So Whole Foods let the janitors determine their own staffing level, interview for new janitors entirely on their own (still subject to law on discrimination, etc), and set their own probation terms: They chose six months. But their entire staff was a third the size of what other companies were hiring to get everything done, so they earned 3 times as much. (But as one member of the team said in the study, if you want to be a part you have to hustle eight hours straight every day, because if the work doesn't get done the team will have to hire and that will reduce everyone's paycheck. So no slackers allowed, and they were self-governing and vigilant against it.)
A different alternative: Committee rule. The ideal committee size (by various academic studies considering numerous traits of how efficient committees are, how subject to deadlock, irrational decisions, etc) is an odd number in the range of 19 (the best are 17,19,21,23).
So what if every level of military management was NOT occupied by an individual, but by a committee of 19 equals? Even if you can't see how to extend that 19 number down to the battlefield level, the idea that the minimum number of people in charge is three equals, and never one dictator is a useful one.
Combine that with Gore's successful ideas about inverted management, and you have a very coup resistant structure and culture: At every level, "how things are done" is pervasive and embedded in the life experience of each individual, the very notion that one person could command others is repellent to them and analogous to slavery.
It isn't enough for the coup to replace the top level, or any number of top levels: They would have to enslave by force the entire structure, and too many people would just walk away or refuse to comply. The resilience would be maximized because the "power" is distributed throughout the system, even to the bottom levels.
We have about 2.8 million civilian employees in the USA (2010). If we organized these into Gore-type inverted management teams of 20, we would end up with five or six levels of management (because $\frac{\log(3000000)}{\log(20)}=4.97$, the $20$ is so each team of 20 can vote 1 member to the next layer up; and still have an odd-numbered team or committee of 19). There may be more than that if the team is sub-divided by specialty or required education; for example if a law degree is required to serve in the judiciary or as prosecuting or defense attorneys, or engineering degrees are required, etc.
Note that the point of the "supervisory" layer above you is not to command you; their job is to make decisions that span a larger scope of projects or scope of time, further into the future. At the bottom my job may be, for example, to interview individual welfare recipients, or be a guard in a prison, or collect garbage, or work on machines in the water purification plant. At the next level up, I am on the committee that schedules and plans budgets for the year and what we will need as our population changes, it is setting the requirements and priorities for what lower level teams must accomplish, and so on. It is not exactly the same as commanding them (and they can demote me back into their workforce on any given day, and replace me with another team member if they are not collectively happy with the job I am doing for them -- I serve at the pleasure of my team).
I will also note that this arrangement requires no "dear leader" loyalty at any level, it does not require vetting people to ensure they are honorable and won't engage in corruption; and there is no "chain of command" that confers singular power upon any individual, ever. We do rely on the statistics that a super majority of people (roughly 85%) lean strongly toward fairness and justice and at least a balanced level of altruism, and their power of immediate recall will likely limit the rise of anybody that aims to screw them or proves unrelentingly selfish or self-serving. At every level, such people can be removed from power by the committee (or work crew) they came from.
You need not allow recusals or abstentions: If a committee member should NOT vote (or cannot vote due to illness or being off the grid somewhere), the majority vote of the 19 people that person represents can substitute for that person's vote.
Also I would note we could organize the entire US voting population of 200M in this way and have only 7 levels of political hierarchy. Perhaps 9 or 10 to account for geographical considerations (like each State being governed, not by a singular governor, but a committee of 19).
This works even in the military, we already have soldiers volunteer for suicide missions for the greater good, and we can screen for those willing to risk their lives in battle. I served my minimum tour to be eligible for the GI Bill, and was assigned to a "first to die" post, should nuclear war break out. I was reconciled to it and in every drill (which we were not allowed to know if it was a drill or not) performed my duty even if it might be the last act of my life: As did everybody else beside me, because if they did not they were removed immediately from the post! Provide enough carrots and the Military will be as full as the citizens decide it should be, and can be run without any single person wielding power over others.
Distributed power will seem more fair to all, and is extremely resistant to coup, hijack, or corruption for self-interest. It is not what people are used to; we evolved from an era when physical strength was paramount and created warlords, kings and emperors that are all basically gang leaders and slavers enforcing their will by brutality (and brutal lieutenants). It doesn't have to be that way.
response to commentary
Albert: Have you considered conflicting interests? etc going to war, moving troops to a certain area. what happens then?
Conflicts between which parties? Every decision is a majority vote of 19 people, likely many more for major decisions like going to war. As for moving troops into harm's way, you must get troops to volunteer, perhaps by volunteering yourself as first to die. No volunteers means you can't do it.
A core part of this is Gore's attitude of no involuntary service; nobody has to do anything they do not want to do; they are always free to walk away. Thus, you cannot order people to their death. That is slavery. If the only possible way for you to prevail in a war is for soldiers to engage in a suicide mission, you should be able to convince enough soldiers to do that for the families and loved ones that will be overrun, enslaved, or killed by the enemy if the mission is not accomplished. If you are some top level committee member and cannot convince others of your plan, tough luck. Find another strategy that fighters will support. If you (Albert) believe this won't work because all soldiers are cowards that won't volunteer to risk or lose their lives, I equally believe you are wrong, and we just have a fundamental irresolvable disagreement about human nature. The power to command people against their will to their death is to me the equivalent of slavery. So is the power to force them to serve in the military against their will. If you can't find volunteers for a mission, find another mission! That is what distributed power looks like; that is what a society of equals looks like.
In this system, such decisions would seldom arise, anyway, they would have to be at least 10 of 19 concluding a suicide mission was needed, and they could be recalled and replaced by the team they came from, if their team disagreed with their vote. Either a strategy would be found that does not demand suicide, or enough would see that the suicide mission was the only path to victory, or that it would save far more lives than it cost, and soldiers would volunteer out of the love they feel for the lives, hopes and dreams that their sacrifice will save. Personally, I think anybody voting for a suicide mission should be considered to be simultaneously volunteering for it.