从git历史记录中删除提交 [英] Removing commit from git history

查看:122
本文介绍了从git历史记录中删除提交的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现我有一年前提交给我的回购的文件,我从来不希望在那里. 在公开我的回购协议之前,我想还原或实际上删除引入了此文件的提交,因此没有人可以访问该文件.

I've discovered that I have a file committed to my repo year ago, which I never wanted to be there. Before I make my repo public I'd like to revert, or actually remove commit which introduced this file so no one will be able to access this file.

我使用了交互式rebase,因此我能够删除不需要的提交.看起来很有希望,但是在从800次提交了约400次提交之后,就出现了冲突.

I've used interactive rebase so I was able to delete unwanted commit. It looked promising but after applying ~400 commits from 800 there is a conflict.

该冲突是由非线性历史记录引起的-存在一个分支,并且两个分支中的一个文件都被修改(假设为feature1和feature2).据我所知,git rebase试图使此并行历史变为线性,但在那些并行,冲突的修改中却失败了.我不想尝试解决此问题,因为还有更多此类情况,要修复所有这些情况将是一项巨大的努力.

The conflict is caused by nonlinear history - there was a branch and one file was modified in both branches (lets say feature1 and feature2). As far as I've noticed git rebase tried to make this parallel history linear and it failed at those parallel, conflicting modifications. I don't want to try to fix it as there are much more such situations and It would be a big effort to fix all of them.

所以我的问题是:是否可以通过重建分支历史记录来简单地删除一个提交而对其他提交没有任何影响?

So my question is: is it possible to simply remove one commit with reconstruction of branch history without any influence on other commits?

我的文件被添加一次,并且从未被修改过,因此没有其他提交触及到它.

My file was added once and was never modified, so none of other commits touch it.

推荐答案

您可以尝试使用filter-branch命令.

git filter-branch --prune-empty --tree-filter 'rm <filename-to-delete> || true' HEAD

如果错误的提交只是添加了要删除的文件,则将跳过整个提交(选项--prune-empty).

If the wrong commit just added the file to remove, the entire commit will be skipped (option --prune-empty).

我已经用一个带有分支和合并的简单回购测试了这个,合并被非常巧妙地保留了下来.因此,您不应期望冲突.先生,filter-branch也会创建一个备份分支. :-)

I've tested this with a simple repo with a branch and a merge, and the merge was quite cleverly preserved. So you should not expect conflicts. And filter-branch creates a backup branch as well, sir. :-)

这篇关于从git历史记录中删除提交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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