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" :
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 :
Add all the files
git add -A
In the third step, we are going to commit all the changes
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" :
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
Rename the current branch to main:
git branch -m main
Finally, pushing all the changes by forcing to publish on the GitHub repository:
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 :)