When working with any project, initially we do not take care of how to write commits and end up with a large unnecessary commit history. In this tutorial, you will learn how to clean the git commit history.

How to remove git commit history

Our approach is to tell Git that your current commit is the initial commit. For that first checkout to the commit, which you want to make as the initial commit. Then run the following commands :

git checkout --orphan latest_branch

git add -A

git commit -am "Initial commit message" #Committing the changes

git branch -D master #Deleting master branch

git branch -m master #renaming branch as master

git push -f origin master #pushes to master branch

git gc --aggressive --prune=all # remove the old files

The above commands will forcefully push the current source code to the master branch as the first command.

Note: You should delete all other branches and tags, because they may still contain the old history.