#SharePointProblems | Koskila.net

Solutions are worthless unless shared! Antti K. Koskela's Personal Professional Blog

How to nuke your Git(Hub) commit history?

koskila
Reading Time 3 min
Word Count 417 words
Comments 2 comments
Rating n/a (0 votes)
View

Ah - this was a fun one. I needed to figure out how to purge, flush and clear your commit history on GitHub. Turns out it isn't as easy as clicking a button in the web user interface - the world is apparently not ready yet! 😁 Luckily, it wasn't that complicated either - and the steps should work for other flavors of git as well.

Background

Always remember to .gitignore your secrets, folks! And if you forget, pray you're not committing to a public repository! And even if it's a private one, purge the history afterward.

So, yeah - I messed up. After recycling the secret I ended up committing in error, I decided to cleanse the world of my mistake, and figure out how to destroy the history of my repository without actually removing the repository itself. And then I decided to publicize my mishap so that others might learn from it :)

On a side note, it's lucky I'm using an old-fashioned version control like git, because if this was on blockchain, the process would've been far more complicated!

Solution

Let's jump into it and nuke your secrets or embarrassing code comments from the commit history! 😁

Time needed: 30 minutes.

How to remove your Git commit history?

  1. Remove all of your local git history

    In Windows:
    cd myrepo rmdir /S .git

    In whatever cool operating system the youngsters now use:
    cd myrepo rm -rf .git

  2. Init a new repository

    Now that the existing history has been destroyed

    git init git add .

  3. .gitignore your files

    At this point, your repository is empty with plenty of code to commit. This time, make sure NOT to commit anything you don't want everyone else to see!

  4. Re-commit your data

    Okay, now you're ready to commit your actual files.

    That can be done somewhat like this:
    git commit -m "Removed history, due to sensitive data"

  5. Push to remote

    The following will add your repo on GitHub as an origin and force push your changes there, overwriting the whole history.

    git remote add origin github.com/username/myrepo git push -u --force origin main


And there you go! Let me know how it goes for you, and which secret you ended up committing by mistake! 😁

For your information, mine was a token used in my Home Assistant configuration - I had .gitignored the secrets.yaml file, but I hadn't properly removed some of the files Home Assistant generates - and one of them contains plenty of credentials. What fun.

References

Comments

Interactive comments not implemented yet. Showing legacy comments migrated from WordPress.
Luan Marques
2021-11-24 14:52:19)
It doesn't actually remove the commits, if you have the hash you can still access them
2021-11-26 13:32:09
This is a very valid point. Same is true for Azure DevOps, where the "nuked" commits can still be browsed from the PR history - wonder how that works for GitHub. 🤔
Whitewater Magpie Ltd.
© 2025
Static Site Generation timestamp: 2025-08-19T05:06:16Z