Git:如何从历史提交中删除文件? [英] Git: How to remove file from historical commit?

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

问题描述

我已经提交id 56f06019(例如)。在那个提交中我偶然提交了大文件(50Mb)。在另一个提交我添加相同的文件,但在正确的大小(小)。现在我的回购当我克隆是太重了:(如何从回购历史中删除该大文件,以减少我的回购大小?

解决方案

Pro Git 书的第9章有关于删除对象



让我简单地概述这里的步骤:

  git filter-branch --index-filter \ 
'git rm --cached --ignore-unmatch path / to / mylarge_50mb_file'\
--tag-name-filter cat - --all

就像前面介绍的重新布局选项一样, filter-branch 是重写操作。必须 - force 推送新的参考。


$ b 过滤器分支方法比 rebase 方法强大得多,因为它


  • 允许您一次处理所有分支/ ref,

  • 可以重命名任何标签即使已添加文件

  • 干净地运行,即使文件是(re),也可以干净地运行
  • )在(a)分支的历史记录中添加/删除了多次
  • 不会创建新的,不相关的提交,而是在修改与它们关联的树时复制它们。这意味着像签名提交,提交注释等内容被保留。



filter-branch 还保留备份,所以回购的大小不会立即减少,除非您过期reflogs和垃圾回收:

  rm -Rf .git / refs / original#谨慎
git gc --aggressive --prune = now#危险


I have commit with id 56f06019 (for example). In that commit i have accidentally commited large file (50Mb). In another commit i add the same file but in the right size (small). Now my repo when i clone is too heavy :( How to remove that large file from repo history to reduce the size of my repo ?

解决方案

Chapter 9 of the Pro Git book has a section on Removing Objects.

Let me outline the steps briefly here:

git filter-branch --index-filter \
    'git rm --cached --ignore-unmatch path/to/mylarge_50mb_file' \
    --tag-name-filter cat -- --all

Like the rebasing option described before, filter-branch is rewriting operation. If you have published history, you'll have to --force push the new refs.

The filter-branch approach is considerably more powerful than the rebase approach, since it

  • allows you to work on all branches/refs at once,
  • renames any tags on the fly
  • operates cleanly even if there have been several merge commits since the addition of the file
  • operates cleanly even if the file was (re)added/removed several times in the history of (a) branch(es)
  • doesn't create new, unrelated commits, but rather copies them while modifying the trees associated with them. This means that stuff like signed commits, commit notes etc. are preserved

filter-branch keeps backups too, so the size of the repo won't decrease immediately unless you expire the reflogs and garbage collect:

rm -Rf .git/refs/original       # careful
git gc --aggressive --prune=now # danger

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

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