从所有Git存储库提交历史中完全删除文件 [英] Completely remove file from all Git repository commit history

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

问题描述

在数次提交之前,我意外地将一个不需要的文件(解析合并时)提交了一个不需要的文件( filename.orig ),直到现在我都没有注意到它。我想从存储库历史记录中完全删除文件。是否有可能重写更改历史记录,使得 filename.orig 永远不会添加到存储库中?

解决方案

如果您的情况不是问题中描述的情况,请不要使用此配方。这个配方是用来修复一个不好的合并,并且将你的好提交重播到一个固定的合并。


$ b 尽管 filter-branch 会做你想做的,这是一个相当复杂的命令,我可能会选择使用 git rebase 来做到这一点。这可能是个人偏好。 filter-branch 可以在一个稍微复杂的命令中完成,而 rebase 解决方案执行等效逻辑

请尝试以下配方:

 #在错误合并的位置创建并检出一个临时分支
git checkout -b tmpfix<合并sha1>

#删除不正确添加的文件
git rm somefile.orig

#提交修改的合并
git commit --amend

#返回主分支
git checkout master

#将主分支重新加入到更正的合并中
git rebase tmpfix

#删除临时分支
git分支-d tmpfix

(请注意,需要一个临时分支,你可以使用'detached HEAD'来做到这一点,但是你需要记下由 git commit --amend 步骤产生的commit id。提供给 git rebase 命令,而不是使用临时分支名称。)


I accidentally committed an unwanted file (filename.orig while resolving a merge) to my repository several commits ago, without me noticing it until now. I want to completely delete the file from the repository history. Is it possible to rewrite the change history such that filename.orig was never added to the repository in the first place?

解决方案

Please don't use this recipe if your situation is not the one described in the question. This recipe is for fixing a bad merge, and replaying your good commits onto a fixed merge.

Although filter-branch will do what you want, it is quite a complex command and I would probably choose to do this with git rebase. It's probably a personal preference. filter-branch can do it in a single, slightly more complex command, whereas the rebase solution is performing the equivalent logical operations one step at a time.

Try the following recipe:

# create and check out a temporary branch at the location of the bad merge
git checkout -b tmpfix <sha1-of-merge>

# remove the incorrectly added file
git rm somefile.orig

# commit the amended merge
git commit --amend

# go back to the master branch
git checkout master

# replant the master branch onto the corrected merge
git rebase tmpfix

# delete the temporary branch
git branch -d tmpfix

(Note that you don't actually need a temporary branch, you can do this with a 'detached HEAD', but you need to take a note of the commit id generated by the git commit --amend step to supply to the git rebase command rather than using the temporary branch name.)

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

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