Git remove

If you find yourself in the same position, the first thing you do is change the visibility of the repo. So, if it's a public repo, make it private. This way, you're sure no one else sees the file while you're working on deleting it.

Next thing, if you aren't already in the project folder, cd into it on git bash or whatever terminal you are using. Let's assume my project folder is My-Project and I have a file named secretFile.json I want to delete.

cd My-Project

then run the following command. You have to include the path to the file and not just the file name.

  --prune-empty --tag-name-filter cat -- --all

replacing config/secretFile.json with the path to the file you want to be removed. In my case, secretFile.json is inside of a folder named config.

Note: The command above deletes the file from your local repository too, so ensure you have copied the content to a notepad or whatever you use before running the command.

Then create and add your file to .gitignore so you don't accidentally push it to GitHub again. You can do that with

echo "name-of-your-file" >> .gitignore
git commit -m 'add file to .gitignore'

Once you are satisfied with your changes, you can then push them to GitHub

git push origin --force --all

And that's it! Your repo history is clean without any trace of your sensitive file.