How to permanently delete a file stored in GIT? [duplicate]

Solution 1:

I always find Guides: Completely remove a file from all revisions feed helpful.

To remove the file called Rakefile:

git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch Rakefile' \
  --prune-empty --tag-name-filter cat -- --all

This command will run the entire history of every branch and tag, changing any commit that involved the file Rakefile, and any commits afterwards. Commits that are empty afterwards (because they only changed the Rakefile) are removed entirely.

Solution 2:

Update for remote repository:

git filter-branch -f --index-filter "git rm -rf --cached --ignore-unmatch FOLDERNAME" -- --all

replace FOLDERNAME with the file or folder you wish to remove from the given git repository.

rm -rf .git/refs/original/

git reflog expire --expire=now --all

git gc --prune=now

git gc --aggressive --prune=now

Now push all the changes to the remote repository

git push --all --force

This would clean up the remote repository.

Solution 3:

You can also use bfg for ease.

The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history:

Removing Crazy Big Files Removing Passwords, Credentials & other Private data

$ bfg --delete-files YOUR-FILE-WITH-SENSITIVE-DATA

Or just replace all the occurrences of some file:

$ bfg --replace-text passwords.txt

check https://rtyley.github.io/bfg-repo-cleaner/ and https://help.github.com/articles/removing-sensitive-data-from-a-repository/

Solution 4:

You can with git filter-branch's --index-filter.