Git: How to delete committed file permanently?

痞子三分冷 提交于 2019-12-23 05:29:27

问题


I have committed a file that shouldn't be in a git repository. I have deleted it and committed the deletion, but I can still access the file via the git history.

How do I delete a committed file permanently, so that there is no trace of it?


回答1:


You can remove permanently a file using filter-branch. It will apply the command in argument in every revision of your repository.

git filter-branch --tree-filter 'rm -rf your_file' HEAD

This detailed post might help you. The git doc is pretty helpful too.

As mentioned in the comments, you will also need a git gc to clean your local commits and then git pushto update your remote repository.




回答2:


This might help:

https://help.github.com/articles/remove-sensitive-data

The following command will do this:

git clone https://github.com/defunkt/github-gem.git
# Initialized empty Git repository in /Users/tekkub/tmp/github-gem/.git/
# remote: Counting objects: 1301, done.
# remote: Compressing objects: 100% (769/769), done.
# remote: Total 1301 (delta 724), reused 910 (delta 522)
# Receiving objects: 100% (1301/1301), 164.39 KiB, done.
# Resolving deltas: 100% (724/724), done.

cd github-gem

git filter-branch --force --index-filter \
  'git rm --cached --ignore-unmatch Rakefile' \
  --prune-empty --tag-name-filter cat -- --all
# Rewrite 48dc599c80e20527ed902928085e7861e6b3cbe6 (266/266)
# Ref 'refs/heads/master' was rewritten

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. Note that you'll need to specify the full path of the file you want to remove, not just its filename.



来源:https://stackoverflow.com/questions/16558782/git-how-to-delete-committed-file-permanently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!