Git: How to clear github repo commit history

I had a repo on github that was set to private because the database connection details were exposed in the code. I recently added some code which moved the sensitive database connection details to an outside file so that the repo could be changed from private to public. However, after setting the repo to public view, I noticed that some previous git commits still included the hard coded database connection details. I obviously need to hide these commits because I don’t want to expose those connection details to the public!

Here is how to “start over” in git with a clean repo and remove all previous code commit details on github:


Step 1: Remove the git history

First, in a Bash window, cd to the folder location on your computer that houses the target git file. Then run this command to clear the history:

rm -fr .git


Step 2: Re-initialize your local .git

The next step is to run these three git commands, in order:

git init

git add .

git commit -m "Re-initialize commit history.


Step 3: Push the git repo back up to github

Run the following command with the URL of your git repo as the fifth argument. Note: This repo URL can be found in your github account by clicking the green “Clone or download” button:

git remote add origin https://github.com/chris-relaxing/wordpress_toc.git

Finally, push this back up to github:

git push -u --force origin master

For example: