Delete your Git history

Sometimes it is useful to clear a repository’s commit history. For example, you might not want your code history from your prototyping phase to be visible on GitHub. Based on this StackOverflow post, here is what I think is the best way to do this:

1. Create a new branch

git checkout --orphan latest_branch

2. Add all files

git add -A

3. Commit all files

git commit -am "commit message"

4. Delete your main branch

git branch -D main

5. Rename your branch to main

git branch -m main

6. Forcefully update your repository

Warning: This is dangerous. Make sure you have another copy of your repository somewhere.

git push -f origin main