git移除文件的最旧版本 [英] git remove oldest revisions of a file

查看:103
本文介绍了git移除文件的最旧版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个33 MB大文件,我想永久删除该文件的最旧版本,所以我只保留最新的X版本。如何做到这一点?

I have a 33 MB large file where I want to permanently delete the oldest revisions of that file, so I only the latest X revisions are kept around. How to do it?

我的裸仓库因此而增长巨大。

My bare repository has grow huge because of it.

我已经尝试了以下..但完全删除文件

I have tried the following.. but it removes the file entirely

git filter-branch --index-filter 'git rm --cached --ignore-unmatch big_manual.txt' HEAD

要识别我的存储库中的大文件,我使用 git-large-blob by Aristotle Pagaltzis

To identify the large files in my repository I use git-large-blob by Aristotle Pagaltzis.

推荐答案

我认为你在正确的轨道上使用你试过的 git filter-branch 命令。问题在于你没有告诉它将文件保存在任何提交中,因此将其从所有提交中删除。现在,我认为没有办法直接告诉 git-filter-branch 来跳过任何提交。但是,由于这些命令是在shell环境中运行的,因此使用shell除去最后的X个修订版本应该不会太困难。像这样:

I think you are on the right track with the git filter-branch command you tried. The problem is you haven't told it to keep the file in any commits, so it is removed from all of them. Now, I don't think there is a way to directly tell git-filter-branch to skip any commits. However, since the commands are run in a shell context, it shouldn't be too difficult to use the shell to remove all but the last X number of revisions. Something like this:

KEEP=10 I=0 NUM_COMMITS=$(git rev-list master | wc -l) \
git filter-branch --index-filter \
'if [[ ${I} -lt $((NUM_COMMITS - KEEP)) ]]; then
     git rm --cached --ignore-unmatch big_manual.txt;
 fi;
 I=$((I + 1))'

$ c $> big_manual.txt 在过去的10次提交中。

That would keep big_manual.txt in the last 10 commits.

就像Charles提到的那样,我不确定这是这是最好的方法,因为你实际上是通过删除旧版本来消除VCS的整个问题。

That being said, like Charles has mentioned, I'm not sure this is the best approach, since you're in effect undoing the whole point of VCS by deleting old versions.

你是否已经尝试过使用 git-gc 和/或 git-repack ?如果不是,那么可能值得一试。

Have you already tried optimizing the git repository with git-gc and/or git-repack? If not, those might be worth a try.

这篇关于git移除文件的最旧版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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