While rallying in Virginia for his first campaign event since leaving office, former president Barack Obama joked about his relationship with his distant relative, former Confederate President Jefferson Davis.

"I'll bet he's spinning in his grave," Obama said when discussing how Americans should address the history of slavery in the U.S.

"My mother — you can trace her lineage — and like I'm a eighth or ninth or tenth or something cousin removed from Jefferson Davis who was the head of the Confederacy."

Obama has known for years about his relationship to Davis and that some of his ancestors on his mother's side were slave owners.

"Think about that," Obama joked.

More:Obama to hit campaign trail for first time since leaving office

Watch:Former president Obama campaigns for Virginia gubernatorial candidate Northam

Related:Mississippi school named after Confederate president to be renamed for Barack Obama

The former president was speaking at a campaign event with Virginia Democratic gubernatorial candidate Ralph Northam. Obama also addressed in a more serious tone the issue of how the U.S. should remember its slave-owning past.

"We shouldn't use the most painful parts of our history just to score political points," Obama said. "We don't rise up by repeating the past, we rise up by learning form the past."

But of his Kenyan lineage — which was the cause of controversy during his presidency when some conservatives, including President Trump, claimed Obama was not born in the U.S. — the Hawaiian-born politician was also able to crack a joke.

"My father was from Kenya, as you know. Well, some of you know. Some of you may have forgotten."

Follow Ryan Miller on Twitter @RyanW_Miller