How to delete all commit history from Github?

How to delete all commit history from Github?

Make your commit history clean

There are many ways to delete the commit history but I have discussed below the most efficient and safest way to do it.

The first question that may strike your mind that why we should delete commit history when working on a project? To make the commit history clean and make it better when someone goes out checking your project on GitHub or Gitlab.

Deleting the .git folder may cause problems in your git repository. If you want to delete all your commit history but want to keep the code as it was previously, it is very safe to do it as in the following:

The first step we are going to do is by switching to the latest branch that is "newbranch" :

  1. Checkout:

    git checkout --orphan newbranch

The second step we are going to do is adding all the files to the staging area and to do that we have the following command :

  1. Add all the files

    git add -A

In the third step, we are going to commit all the changes

  1. Commit the changes

    git commit -am "commit all the changes"

In the fourth step, we are deleting the main branch that was our primary branch before switching to the "newbranch" :

  1. Delete the branch:

    git branch -D main

In the fifth step, we are to going to rename our current branch name back to the "main" branch

  1. Rename the current branch to main:

    git branch -m main

Finally, pushing all the changes by forcing to publish on the GitHub repository:

  1. Finally, force update your repository

    git push -f origin main

The above mentioned practice is the best as I have used it few times in my development journey so I would really love if you use the above mentioned process to make your commit history clean.

Thanks for reading the blog and feel free to provide feedback :)