如何从本地和远程每个分支的 Git 提交历史记录中删除文件? [英] How do I delete a file from the Git Commit history from every branch locally and remotely?

查看:52
本文介绍了如何从本地和远程每个分支的 Git 提交历史记录中删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是 Git 新手,

Git Newbie here,

如何从我的 Git 提交中删除文件的所有痕迹 - 例如在本地和远程的每个分支的任何地方删除它.

How do I remove all traces of a file from my Git commits - e.g. delete it everywhere in every branch, both locally and remote.

我看过的大多数文章和 Stack Overflow 的东西都说明了这种方法的一些变化:

Most of the articles and Stack Overflow stuff I've seen states that some variation of this is the way to go:

 git filter-branch --tree-filter 'rm -f file_to_remove.py' HEAD

 git filter-branch --tree-filter 'rm -f file_to_remove.py' ..HEAD

 git filter-branch --force --tree-filter 'rm -f file_to_remove.py' -- --all

等等...

但是,我似乎无法工作.

But, I can't seem to get any to work.

一方面,我不断收到这样的错误或其他分支的另一个类似错误.

For one, I keep getting an error like this one or another similar error for other branches.

WARNING: Ref 'refs/remotes/origin/master' is unchanged" 

我运行了最后一个,它经历了所有的修改,看起来"好像在做某事,但后来我执行了....

I ran the last one and it went through all the revisions and "looked" like it was doing something, but then I executed....

git show <<REVISION_HASH_NUM>>:file_to_remove.py 

这表明该文件仍然存在于提交历史中.同样,该文件的 Git 提交历史仍然存在于 TFS 中.

Which shows that the file is still present in the commit history. Likewise, my Git commit history for that file is still present it TFS.

此外,还有 247 个提交和 16 个以上的其他开发人员使用此存储库.我不能对 Master 分支进行核攻击.

Also, there are 247 commits and 16+ other developers that use this repo. I can't nuke the Master branch.

附言请不要向我推荐其他 Stack Overflow 帖子.我已经看过其中的大部分了.另外,请不要说要改写我的问题.如果我知道的足够多,可以正确地提出问题,那么我就可以自己解决问题.

P.S. Please don't refer me other Stack Overflow posts. I've already looked at the majority of them. Also, please don't say to rephrase my question. If I knew enough to ask the question correctly, then I'd know enough to fix it myself.

提前致谢!

推荐答案

Fist:比 git filter-branch 简单得多的工具是 BFG Repo-Cleaner - git filter-branch 的手册中已经提到了.此工具使删除对象变得更加容易.

Fist: A much simpler tool than git filter-branch is the BFG Repo-Cleaner - it is already mentioned in the manual of git filter-branch. This tool makes deleting objects much easier.

第二:你的测试

git show <<REVISION_HASH_NUM>>:file_to_remove.py 

是有缺陷的:即使在过滤现有提交的内容之后也不会改变.取而代之的是 filter-branch 创建新的提交,从而创建新的哈希.此外,旧提交不会立即删除 - 它们只是无法从重写的分支和标签中访问.一段时间后,它们会被垃圾收集.

is flawed: Even after filtering the contents of the existing commits are not changed. Instead filter-branch creates new commits and hence new hashes. Also the old commits are NOT deleted immediately - they are just unreachable from the rewritten branches and tags. After a while they are garbage collected.

因此,一个简单的测试是使用选项 --no-hardlinks 克隆您的存储库并检查那里的历史记录.

So a simple test is to make a clone of your repo using the option --no-hardlinks and check the history there.

第三:

此外,还有 247 个提交和 16 个以上的其他开发人员使用此存储库.我不能对 Master 分支进行核攻击.

Also, there are 247 commits and 16+ other developers that use this repo. I can't nuke the Master branch.

不可能 - 提交的内容与哈希相关联,反之亦然.如果您更改内容,您将更改哈希值 - 您正在重写历史"(Google 搜索词).这 16 个以上的开发者会讨厌你 :-)

No chance - the content of a commmit is tied to the hash and vice versa. If you change the content you change the hash - you are "rewriting the history" (Google search term). These 16+ developers will hate you :-)

这篇关于如何从本地和远程每个分支的 Git 提交历史记录中删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆