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

Rod*_*igo 82 git git-rewrite-history

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

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

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

如何最终删除此文件并节省磁盘空间?

Leo*_*Leo 156

您可以使用git filter-branch命令执行此操作,如下所示:

git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD
Run Code Online (Sandbox Code Playgroud)

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

  • 我正在使用Windows.我不得不将单引号换成双引号. (8认同)
  • 我有这样的错误:`无法重写分支:你有未分阶段的变化 (6认同)
  • 1. `git filter-branch --tree-filter 'rm -rf path/to/your/file' HEAD` 2. 如果出现此错误:无法重写分支:您有未暂存的更改。3. `git stash save` 4. `git Push origin master --force` (4认同)
  • 请注意,它还会删除当前检出的版本,因此如果需要保留,请确保备份. (3认同)
  • @rleelr我认为github页面提供了一个干净的解决方案,而不会影响其他文件:https://help.github.com/articles/removing-sensitive-data-from-a-repository/ (2认同)

Jar*_*Par 26

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


top*_*pek 17

你可以从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
Run Code Online (Sandbox Code Playgroud)