Resetting a Git repository and clearing its history is a way to start fresh while preserving the current state of files. This process is useful when you want to remove all prior commit history but keep the latest state of the project. The main steps include creating a new branch to store the current state of your files, deleting the old Git history, and reinitializing the repository with a new commit. This approach allows the repository to lose all previous commit logs and histories, making the repository lightweight and simpler without the accumulated history.
To perform this, you can start by creating a new branch (such as temp-branch) and switching to it. Then, delete the old .git folder, which removes the repository’s history entirely. Afterward, reinitialize the repository by running git init, add all files with git add ., and make a new initial commit using git commit -m "Initial commit". Finally, if you’re pushing to a remote repository, force-push this new state using git push -f origin main or your target branch. This process is useful for cleaning up the repository but should be used cautiously, especially if others rely on the repository’s history.
Clear history from;
rm -rf .git
Recreate the repository from the current content only;
git init
git add .
git commit -m "Initial commit"
Push to remote github repository & overwrite previous history;
git remote add origin git@github.com:<YOUR ACCOUNT>/<YOUR REPOS>.git
git push -u --force origin master