While serving as a sentry with French forces in the Argonne Forest in 1918, a black American private fought off German attackers. ­Unfazed by his wounds, he hurled grenades until they ran out, shot his rifle until it jammed, used his rifle as a club until it broke and ­finally used a bolo knife until ­reinforcements arrived.

The French recognized Henry Johnson’s heroism with a Croix de Guerre, while the United States gave him the Medal of Honor — posthumously, almost 100 years later.

Johnson is a part of a long African American military tradition of exceptional devotion to a country that, through its history, ­denied blacks their rights and discriminated against and humiliated its black soldiers.

These were the men of the iconic 54th Massachusetts Volunteer Infantry in the Civil War and of the Harlem Hellfighters in World War I, of the Buffalo Soldiers on the frontier and of the legendary Tuskegee airmen in World War II.

They were always fighting a two-front war — against the enemy in battle and against prejudice at home. They were fighting to prove their mettle and that they were as — or more — American than their white countrymen.

They hoped that their patriotic commitment would loosen the grip of racist repression, and they were disappointed, often cruelly so. Still, they volunteered and, when given the opportunity, fought.

As one observer puts it, “African-American military service is older than the United States itself.” Blacks fought in colonial militias in the French and Indian War, and then in the Revolutionary War and the War of 1812 (although slavery meant they didn’t fight reliably on the American side).

Whites resisted arming black men for fear of rebellions, and they were banned from the army. But it became obvious during the Civil War that they were a ready and willing extra increment of manpower that the Union couldn’t ignore.

Wary of offending white opinion in the North, Lincoln took cautious steps before embracing black soldiers. He said that at the conclusion of the war, “there will be some black men who can remember that with silent tongue, and clenched teeth, and steady eye, and well-poised bayonet, they have helped mankind on to this great consummation; while, I fear, there will be some white ones, ­unable to forget that, with malignant heart, and deceitful speech, they have strove to hinder it.”

Roughly 180,000 black soldiers served, about 9 percent of all Union forces. The vast majority were from slave states. In joining up, they risked re-enslavement, or worse, if captured by Confederate forces. In the end, they helped smash the slave system that had held many of them in bondage shortly before their military service.

Of course, the South fashioned a new system of racial repression. At the outset of World War I, W.E.B. Du Bois strongly supported black enlistment, in the hopes that the sacrifice would lead to better treatment.

“Let us, while this war lasts, forget our special grievances and close our ranks shoulder to shoulder with our own white fellow citizens,” he wrote.

African Americans made up more than 200,000 of the 2 million-strong American expeditionary force. But they got more respect from French commanders, who trained and put them on the front lines, than they did from their own. And they returned home to race ­riots and lynchings.

The dashed hopes of black soldiers throughout much of our history makes their faithfulness all the more extraordinary.

William Carney was born a slave in Virginia in 1840 and made it to freedom in Massachusetts. He joined the 54th Massachusetts Infantry Regiment and participated in the harrowing attack on Fort Wagner in South Carolina, depicted in the film “Glory.”

Carney took custody of the flag when the color sergeant was shot down. He held it up under heavy fire and despite serious wounds, returning it to Union lines. “Boys,” he said, “the old flag never touched the ground.”