DEV Community

Luis Perez
Luis Perez

Posted on

How to clear commit history using GIT

I am working in a presentation to Hotmart Dev team and it will basically have a hands on during this presentation and I will be sharing the final code on Github.

But I don't want them to follow each step I made so I decided to clear the whole commit history.

I obviously had to search because I never had a need to clear the whole commit history and I found this on Stack Overflow from 2014 and edited in 2020:

https://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-github

The answer is pretty clear how I had to do it, so I am sharing here.

Let's see how it was done!

Checkout
git checkout --orphan latest_branch

Add all the files
git add -A

Commit the changes
git commit -am "commit message"

Delete the branch
git branch -D main

Rename the current branch to main
git branch -m main

Finally, force update your repository
git push -f origin main

That's all folks!

Top comments (2)

Collapse
 
yoursunny profile image
Junxiao Shi

Last time I force-pushed to the main branch, people started screaming up and down the hallway. Since then, I configured branch protection rule in GitHub, and it's no longer possible to force-push or clear commit history.
I keep my history clean since the beginning, and never need to cleanup upon release.

Collapse
 
waylonwalker profile image
Waylon Walker

I've been there, where you just need less history to big everything down.