Former Bad Boy rapper G. Dep was sentenced this morning to 15 years prison for a two-decade old fatal gunpoint mugging — a cold-case murder that was only solved when he walked into a Harlem precinct two years ago, turning himself in so he could square himself, he has explained, with God.

“The circumstance of your being before the court now suggests to me a maturity and decency that wouldn’t have been evident at the time,” Manhattan Supreme Court Justice Michael Obus said of the sentence, the lowest allowed by law.

“It was the right thing to do, even though it landed you in the position you find yourself in now,” the judge added, calling the rapper by his given name, Trevell Coleman.

When he walked into a Harlem precinct two years ago, Coleman — who had had success as a protege of Sean “Puffy” Combs in the late ’90s — was a married dad of three and in treatment for an addiction to PCP. As part of turning his life around, he was determined to take responsibility for a crime that had continued to gnaw at him, his lawyer, Anthony Ricco, has explained. And he has never backed down.

“My hope is that Trevell never wavers from his decision,” Ricco said at the poignant sentencing. “Fifteen years is a long time. Fifteen years in the penitentiary for putting yourself there is an even longer time.”

Coleman, 37, has suffered the mockery and insults of his fellow prisoners, the lawyer noted, and in the press as well, for confessing to a shooting that he had essentially gotten away with, only to find out that his victim had died and that he had turned himself in for murder.

“He’s gotten the scorn of other inmates, who called him stupid — ‘Look what happened when you open your mouth,'” the lawyer said. “His wife questioned him — ‘Trevell, what about us?'”

Coleman and his wife were raising twin sons of kindergarten age. On the day of his April verdict, the wife — who had urged Coleman not to confess — had wondered to the lawyer about how the boys will manage now, without their father.

“My hope is that their sons will grow up to be as proud of their father as boys can ever be, for fixing what he did,” Ricco said.

“They will have a father who has honor.”

In confessing, the rapper had told cops that when he was 17, or 18, or maybe 19, he had shot a man three times during a botched mugging at Park Avenue and 114th Street. He didn’t know if the man lived or died.

Authorities scoured old records, much of it on microfilm, and matched the confession to the cold-case fatal shooting of John Henkel, gunned down on Oct. 19, 1993 at the same address. Coleman was

Hard-ball-playing prosecutors refused to offer Coleman a plea to a lesser charge, for which less prison — even a few years less — would be mandated.

The outcome is fair, said prosecutor David Drucker, who did argue in favor of the minimum sentence today.

There was “A totally innocent victim,” Drucker said. “The defendant shot him three times in the torso, killing him, and then he biked off leaving the victim to die.”

Coleman has lived a non-violent life, turned himself in for all the right reasons, and solved a murder that would never have been off the books otherwise, he noted.

Still, “He should be convicted of the crime he committed, murder in the second degree,” the prosecutor said.

“I’m just trying to get right with God,” Coleman had told The Post in an exclusive jailhouse interview back in December 2010. He made no statement at his sentencing today, and nodded somberly to his family members as he was led out of court in handcuffs.