DEV Community

Nic
Nic

Posted on • Originally published at coderscat.com on

How to delete a file permanently in git

Alt Text

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:

git filter-branch

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

Enter fullscreen mode Exit fullscreen mode

Remember to add your sensitive file to .gitignore:

$ echo "./config/password" >> .gitignore
$ git add .gitignore
$ git commit -m "Add password to .gitignore"

Enter fullscreen mode Exit fullscreen mode

Then you’d better push to remote:

$ git push --force --all
$ git push --force --tags
Enter fullscreen mode Exit fullscreen mode

Tell your collaborators to rebase:

$ git rebase
Enter fullscreen mode Exit fullscreen mode

BFG repo-cleaner

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 git filter-branch.

Note : 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 ./config/password:

$ brew install bfg ## install bfg

$ git rm config/password
$ git commit -am"remove password"

$ bfg --delete-files password ## remove from history

Enter fullscreen mode Exit fullscreen mode

Then use git push to push to the remote repository.

The post How to delete a file permanently in git appeared first on CodersCat.

Discussion (1)

Collapse
awwsmm profile image
Andrew (he/him)

Tell your collaborators to rebase:

$ git rebase

...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.