Remove sensitive data/file from GitHub(from any commit)
Ohh Shit! I have pushed the SSH Key to Github.
Yeah, If you want to know how you can remove or delete the sensitive info from Github that you have pushed in you last or older commits then you are at the right place. In this article, We will understand how you can remove any file that you have pushed ever to Github repo. Yes, you read it right we are going to remove the faulty “file”. So, If your file contains other data(not sensitive)too then please make a copy of the file to another path/place. We will use git filter-branch
to get it from history. Use the below commands to remove the file
$ cd <your_git_repository>
$ git checkout <faulty-branch/master>
$ git filter-branch --force --index-filter "git rm --cached --ignore-unmatch PATH_TO_YOUR_FAULTY_FILE" HEAD
//Above command removes the faulty-file from 1st commit to Head commit.
Yeah! you have removed the faulty file from all the commits. Now, Only if you have a copy of your faulty file and you want to push it again then first remove the sensitive information from the file and then add it to git
git add <name_of_your_modified file>
Now you have to commit and push the changes(commit’s change or file’s changes)
git commit -m "remove sensitive data"
git push origin --force --all //or --all can replace by --tags
Hurray! Check your Github repo(refresh it), there will be no more sensitive information in any commit or file.
Thanks for the read. I hope it helps you. Follow me for the other technical informative blogs.