我怎样才能从git仓库中完全删除文件? [英] How can I completely remove a file from a git repository?

查看:207
本文介绍了我怎样才能从git仓库中完全删除文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我注意到我的文本编辑器使用的项目文件(以及其他一些垃圾文件)为项目添加了git存储库。因为它们实际上并不是项目的一部分,所以我想删除它们,但是 git rm 不会从存储库中删除旧版本,而且我也找不到其他任何东西这看起来很有希望。

解决方案

您需要的工具是 git filter-branch 。其用法在这里有描述,但基本上是:

  $ git filter-branch --tree-filter'rm -f my_file'HEAD 

会从每个 提交中移除my_file。注意,这会重写每次提交,所以如果你推入一个远程仓库,你必须(a)强制更新,(b)从你那里拉出的其他人现在将有重复的提交(因为你重写了历史记录),如 git rebase 手册页


I noticed recently that the project files my text editors use (along with some other junk) got added the git repository for the project. Since they aren't actually part of the project, I'd like to remove them, but git rm doesnt remove the old versions from the repository, and I couldnt find anything else that looks promising.

解决方案

The tool you want is git filter-branch. Its usage is described here, but basically:

$ git filter-branch --tree-filter 'rm -f my_file' HEAD

will remove "my_file" from every commit.

Notice that this rewrites every commit, so if you push into a remote repository, you have to (a) force the update, and (b) everyone else who pulled from you will now have duplicate commits (since you rewrote the history), as described on the git rebase man page.

这篇关于我怎样才能从git仓库中完全删除文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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