If you want to remove a file permanently , you must wish to there are not change log left in Git history. Otherwise, others still could see the content in the Git repository.
There are two methods for this task:
Suppose you want to remove
./config/passwd from Git:
$ git filter-branch --force --index-filter \ 'git rm --cached --ignore-unmatch ./config/password' \ --prune-empty --tag-name-filter cat -- --all
Remember to add your sensitive file to .gitignore:
$ echo "./config/password" >> .gitignore $ git add .gitignore $ git commit -m "Add password to .gitignore"
Then you’d better push to remote:
$ git push --force --all $ git push --force --tags
Tell your collaborators to rebase:
$ git rebase
Use BFG Repo-Cleaner.
BFG provides a faster, simpler alternative to
git filter-branch for removing sensitive data. It’s very quickly, usually 10 – 720x faster then
bfg will leave your latest commit untouched. It’s designed to protect you from making mistakes. You should explicitly delete the file, commit the deletion, then clean up the history to remove it.
Suppose you want to remove the file
$ brew install bfg ## install bfg $ git rm config/password $ git commit -am"remove password" $ bfg --delete-files password ## remove from history
git push to push to the remote repository.
The post How to delete a file permanently in git appeared first on CodersCat.
Top comments (1)
...this is the critical bit. If anyone still has the "bad" file and pushes to the remote repo again, it will be added back to the commit history. This is why you should always triple check what you're pushing to publicly-accessible repos. Anyone who's ever cloned it will have all the files you ever added, even ones you delete in this way.