How to remove all git commit history?

Its usually not a great idea to remove all history for a repository as the history represents all changes for a project. There may be scenarios where this is needed, like commit history containing sensitive information or a disorganized commit history that needs cleaning. This approach will essentially tell git that your current commit is the initial commit. Please note that these steps are final and will remove all commit history for a repo.

Delete Commit History in Git Repository

Checkout/Create Orphan Branch – Create a new orphan branch in your git repository. This branch will not show in git branch command.

git checkout --orphan latest_branch

Add All The Files to Branch – Add all existing files to your newly created branch.

git add -A

Commit The Changes – After adding all files to your new branch, commit the changes

git commit -am "your new commit message"

Delete Main (Default) Branch – Now you can delete the main (default) branch from your git repository. This step is permanent.

git branch -D main

Rename The Current Branch – After deleting the old main (default) branch, rename the newly created branch to main.

git branch -m main

Push Changes – All these changes are completed on your local repository and now its time to force push these changes to your remote repository.

git push -f origin main

Now you should only see your new commit in the history of your git repository.

Leave a Reply