Posted on One min read

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

1. Checkout

git checkout --orphan latest_branch

For the OPTIONS --orphan: Create a new orphan branch, named latest_branch, started from HEAD and switch to it. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from all the other branches and commits.

2. Add all the files

git add -A

3. Commit the changes

git commit -am "commit message"

4. Delete the branch

git branch -D master

5. Rename the current branch to master

git branch -m master

6. Finally, force update your repository

git push -f origin master

PS: This will not keep your old commit history around.