How to reset Git repository without deleting it

Summary: This wiki page shows how to bring back a Git repositoy to it's initial state, without deleting it.
Date: 2 July 2026

I have a training repo, which I use for testing purposes and more, and after playing with the repo, I want to clean it up without deleting it. This will remove any accidentally commited secret, as well as all the history from the repository without removing the settings on the repository. To do so, follow these commands:

# Get the initial commit hash and reset to that commit
git reset --hard db8137f3454430ee514c98962217c2445b539176
# Expire all reference logs
git reflog expire --expire=all --all
# Cleanup unnecessary files and optimize the local repository
git gc --prune=now
# Push the changes to the remote
git push origin --force

That's it.

This wiki has been made possible by: