Removing entire commit history from a Git repository


Sometimes you may want to remove the entire history of a Git project and just keep the latest code in it. You can simply achieve this by creating a new repository and do a single commit with the latest code into the new repository. But in case you can't do that and you want to keep the existing repository only then you can follow the below steps to achieve it.

1. Remove .git folder

rm -rf .git

2. init git

git init

3. Commit your current files

git add .
git commit -m "Initial commit"

4. Push it to the remote repository

git remote add origin <origin url>
git push -u --force origin master

Example origin url: https://github.com/onecompiler/cheatsheets.git

With these steps, you can push keep your latest code into the same repository and discard entire history.