Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove GitHub commit along with it's history?

My friend made a config file, holding our apikey/token/secrets, and a .gitignore to ignore said file. Someone accidentally pushed to the master branch with the config file saved as another name and is now showing.

I was wondering if there was a way to delete that commit and the history of that commit.

I have seen this (below) and I understand this will delete the commit and re-push everything. But it still shows the config info via history/changes:

git reset --hard hash# 

git push -f origin branch
like image 427
Kim Avatar asked Oct 29 '22 05:10

Kim


1 Answers

git-filter-branch can help you in your case, as follows:

Replace PATH-TO-YOUR-CONFIG-FILE with the path to your config file you want to remove, not just its filename.

git filter-branch --force --index-filter \
'git rm --cached --ignore-unmatch PATH-TO-YOUR-CONFIG-FILE' \
--prune-empty --tag-name-filter cat -- --all

Add your config file to .gitignore to ensure that you don't accidentally commit it again, commit and push it, following below commands:

echo "YOUR-CONFIG-FILE" >> .gitignore
git add .gitignore
git commit -m "Add YOUR-CONFIG-FILE to .gitignore"
git push origin --force --all
like image 84
Arpit Aggarwal Avatar answered Nov 15 '22 07:11

Arpit Aggarwal