Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove file from git repository history

Tags:

git

github

I checked in (into github) some sensitive files by mistake. To remediate this, I followed the instructions here and ran the commands:

git filter-branch --force --index-filter "git rm --cached --ignore-unmatch settings.json" --prune-empty --tag-name-filter cat -- --all
echo "settings.json" >> .gitignore
git add .gitignore
git commit -m "Add settings.json to .gitignore"
git push origin --force --all
git push origin --force --tags
git for-each-ref --format="delete %(refname)" refs/original | git update-ref --stdin
git reflog expire --expire=now --all
git gc --prune=now

I can however go to my commit history to see the deleted file.

To fix this issue, how can I delete the file from github commit history?

enter image description here

enter image description here

like image 268
Ajit Goel Avatar asked Jan 14 '20 05:01

Ajit Goel


People also ask

How do I remove a file from a github repository?

Browse to the directory in your repository that you want to delete. In the top-right corner, click , then click Delete directory. Review the files you will delete. At the bottom of the page, type a short, meaningful commit message that describes the change you made to the file.


1 Answers

Try instead to use the best practice is to use the new tool git filter-repo which replaces BFG and git filter-branch.

Note: if you get the following error message when running the above-mentioned commands:

Error: need a version of `git` whose `diff-tree` command has the `--combined-all-paths` option`

it means you have to update git.


See "Path based filtering":

git filter-repo --path settings.json --invert-paths

Then git push --force

No need for all those repack/gc/prune at the end: the tool does the cleanup for you.

like image 188
VonC Avatar answered Oct 11 '22 08:10

VonC