我使用Git和Github(私人帐户)。
On this thread显示类似的内容,但这里是一个添加到功能分支的旧文件,该分支合并为一个 开发分支最后合并到掌握,因此,很多 改变已经完成。所以它不一样,需要改变 历史记录,并隐藏该文件以保护隐私。
答案 0 :(得分:14)
如果您最近提交了该文件,或者该文件在一次或两次提交中已更改,那么我建议您使用rebase
和cherrypick
删除该特定提交。< / p>
否则,您必须重写整个历史记录。
git filter-branch --tree-filter 'rm -f <path_to_file>' HEAD
如果您对这些更改感到满意并且确保一切正常,则需要更新所有远程分支 -
git push origin --force --all
注意: - 这是一项复杂的操作,你必须知道自己在做什么。首先尝试在演示存储库中执行它以查看它是如何工作的。您还需要让其他开发者了解它,以便他们不会在平均时间内做出任何改变。
答案 1 :(得分:9)
我找到了这个答案,它有所帮助:
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch path_to_file" HEAD
在这里https://myopswork.com/how-remove-files-completely-from-git-repository-history-47ed3e0c4c35
答案 2 :(得分:6)
.gitignore
文件中,不要忘记提交该文件:-)您可以使用以下站点:http://gtiignore.io为您生成.gitignore
并将所需的路径添加到二进制文件/文件夹
将文件添加到.gitignore
后,您可以使用BFG删除“旧”二进制文件。
How to remove big files from the repository
您可以使用git filter-branch
或BFG。
https://rtyley.github.io/bfg-repo-cleaner/
BFG Repo-Cleaner
git-filter-branch的替代方法。
BFG是git-filter-branch的一种更简单,更快速的替代方法,用于从Git存储库历史记录中清除不良数据:
*删除疯狂的大文件*
*删除密码,凭据和其他私人数据
在所有这些示例中,bfg是java -jar bfg.jar的别名。
# Delete all files named 'id_rsa' or 'id_dsa' :
bfg --delete-files id_{dsa,rsa} my-repo.git
答案 3 :(得分:2)
git
建议使用git-filter-repo(执行git filter-branch
命令时)。为什么它比其他任何替代方法(https://github.com/newren/git-filter-repo#why-filter-repo-instead-of-other-alternatives)都好,但有一长串清单,我的经验是,它非常简单且非常快。
此命令从所有分支的所有提交中删除文件:
git filter-repo --path <path to the file or directory> --invert-paths
可以使用多个--path
参数指定多个路径。您可以在此处找到详细的文档:
https://www.mankier.com/1/git-filter-repo
答案 4 :(得分:1)
我使用this GitHub article来使用它,这导致我执行此命令(类似于公认的答案,但更可靠):
git filter-branch --force --index-filter "git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA" --prune-empty --tag-name-filter cat -- --all
答案 5 :(得分:0)
删除文件并重写对已删除文件所做的提交的历史记录(这将根据您提交的文件创建新的提交哈希):
git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch PATH-TO-YOUR-FILE-WITH-SENSITIVE-DATA' --prune-empty --tag-name-filter cat -- --all
现在强制推送仓库:
git push origin --force --all
现在告诉您的合作者rebase
。
答案 6 :(得分:0)
使用bfg repo-cleaner软件包是#warning COMPILING CUDA VERSION
的另一可行选择。显然,它也更快...