如何永久删除存储在 GIT 中的文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2004024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to permanently delete a file stored in GIT?
提问by mrblah
I backed up my database to GIT just so I could get the db at my home computer.
我将我的数据库备份到 GIT,这样我就可以在我的家用电脑上获取数据库。
I don't want this file to be versioned, it was just a 1 time thing really.
我不希望这个文件被版本化,这真的只是 1 次。
Can I delete it for good so GIT doesn't keep track of it going forward or historically?
我可以永久删除它,这样 GIT 就不会跟踪它的未来或历史吗?
回答by Alan Haggai Alavi
I always find Guides: Completely remove a file from all revisions feedhelpful.
我总是发现指南:从所有修订提要中完全删除文件很有帮助。
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.
删除名为 的文件
Rakefile
:git filter-branch --force --index-filter \ ? 'git rm --cached --ignore-unmatch Rakefile' \ ? --prune-empty --tag-name-filter cat -- --all
此命令将运行每个分支和标签的整个历史记录,更改涉及文件的
Rakefile
任何提交,以及之后的任何提交。之后为空的提交(因为它们只更改了 Rakefile)将被完全删除。
回答by Andrzej Jozwik
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.
将 FOLDERNAME 替换为您希望从给定 git 存储库中删除的文件或文件夹。
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.
这将清理远程存储库。
回答by dezhi
You can also use bfg for ease.
您也可以轻松使用 bfg。
The BFG is a simpler, faster alternative to git-filter-branch for cleansing bad data out of your Git repository history:
BFG 是 git-filter-branch 的更简单、更快的替代方案,用于清除 Git 存储库历史记录中的不良数据:
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/
检查https://rtyley.github.io/bfg-repo-cleaner/和https://help.github.com/articles/removing-sensitive-data-from-a-repository/
回答by jamessan
You can with git filter-branch's --index-filter
.
您可以使用git filter-branch的--index-filter
.
回答by Saket Milan
I would like to share most simplest and easy to understand solution which worked for me.
我想分享对我有用的最简单易懂的解决方案。
First clone a fresh copy of your repo, using the --mirror flag:
首先使用 --mirror 标志克隆您的 repo 的新副本:
git clone --mirror https://github.com/username/youproject.git
Then Download latest version of BFG jar file from https://rtyley.github.io/bfg-repo-cleaner/rename it as bfg.jar and paste it inside YourRepoName.git folder.
然后从https://rtyley.github.io/bfg-repo-cleaner/下载最新版本的 BFG jar 文件,将其重命名为 bfg.jar 并将其粘贴到 YourRepoName.git 文件夹中。
Then run following lines in git bash.
然后在 git bash 中运行以下几行。
java -jar bfg.jar --delete-files yourfilename
(only file name is needed, no need to mention path)
java -jar bfg.jar --delete-files yourfilename
(只需要文件名,无需提及路径)
git reflog expire --expire=now --all && git gc --prune=now --aggressive
(it will strip out the unwanted dirty data which has been expelled out due to above command)
git reflog expire --expire=now --all && git gc --prune=now --aggressive
(它将去除由于上述命令而被驱逐的不需要的脏数据)
I faced issue here. My project was having open pull requests. If there are any open pull requests then you need to run this command
我在这里遇到了问题。我的项目有开放的拉取请求。如果有任何打开的拉取请求,则您需要运行此命令
git show-ref | cut -d' ' -f2 | grep 'pull-request' | xargs -r -L1 git update-ref -d
After this you can simply push master branch.
在此之后,您可以简单地推送 master 分支。
git push -u origin master
Its done. But remember to keep a copy in local of old repository before performing above action. All unmerged branches and open pull requests may get deleted.
完成。但是请记住在执行上述操作之前在旧存储库的本地保留一个副本。所有未合并的分支和打开的拉取请求可能会被删除。
I reduced my repo size from 40mb to 4mb by removing unwanted apk files which got pushed with my commits.
通过删除随我的提交推送的不需要的 apk 文件,我将我的 repo 大小从 40mb 减少到 4mb。