如何删除在 git 中错误提交的大文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8083282/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 12:16:20  来源:igfitidea点击:

How do I remove a big file wrongly committed in git

gitgit-rewrite-history

提问by Rodrigo

Possible Duplicate:
How to purge a huge file from commits history in Git?

可能的重复:
如何从 Git 的提交历史记录中清除一个大文件?

I did a stupid thing. Imagine that I committed a 100MB file. Then I see this and delete this file and commit again. This is a normal procedure to delete a file.

我做了一件蠢事。想象一下,我提交了一个 100MB 的文件。然后我看到这个并删除这个文件并再次提交。这是删除文件的正常过程。

But now the side effect is that my history is heavy because it's saved this large file (I believe this is why it is heavy). I am only using local git, so I do not synchronize in any server.

但是现在的副作用是我的历史记录很重,因为它保存了这个大文件(我相信这就是它很重的原因)。我只使用本地 git,所以我没有在任何服务器中同步。

How can I definitively remove this file and save disk space?

如何彻底删除此文件并节省磁盘空间?

回答by Leo

You can do it using the git filter-branchcommand, like this :

您可以使用 gitfilter-branch命令来完成,如下所示:

git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD

You can find more documentation here http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository

您可以在此处找到更多文档http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository

回答by JaredPar

The command you are looking for is filter-branch. It allows you to permanently remove files from an enlistment. This blog has a great tutorial on how to remove problematic files from the repository

您正在寻找的命令是filter-branch. 它允许您从登记中永久删除文件。这个博客有一个关于如何从存储库中删除有问题的文件的很棒的教程

回答by topek

You can take this great script from David Underhillto remove the file from the git repository:

您可以使用David Underhill 的这个很棒的脚本从git 存储库中删除该文件:

#!/bin/bash
set -o errexit

# Author: David Underhill
# Script to permanently delete files/folders from your git repository.  To use 
# it, cd to your repository's root and then run the script with a list of paths
# you want to delete, e.g., git-delete-history path1 path2

if [ $# -eq 0 ]; then
    exit 0
fi

# make sure we're at the root of git repo
if [ ! -d .git ]; then
    echo "Error: must run this script from the root of a git repository"
    exit 1
fi

# remove all paths passed as arguments from the history of the repo
files=$@
git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch $files" HEAD

# remove the temporary history git-filter-branch otherwise leaves behind for a long time
rm -rf .git/refs/original/ && git reflog expire --all &&  git gc --aggressive --prune