2011-11-10 21 views
55

Possible Duplicate:
How to purge a huge file from commits history in Git?如何刪除一個大文件中的git

我幹了件蠢事錯誤犯。想象一下,我提交了一個100MB的文件。比我看到這個並刪除這個文件並再次提交。這是刪除文件的正常過程。

但現在副作用是我的歷史很沉重,因爲它保存了這個大文件(我相信這就是它的重要原因)。我只使用本地git,所以我不會在任何服務器上同步。

我該如何明確地刪除此文件並節省磁盤空間?

+1

查看接受的答案爲我的問題http://stackoverflow.com/questions/796983 1/drop-old-commit-git-rebase-causes-merge-conflicts –

+0

使用BFG repo-cleaner是一種更簡單,更快速的替代'git-filter-branch'的替代方法,專門用於從Git歷史記錄中刪除不需要的文件。請參閱http://stackoverflow.com/a/17890278/438886 –

回答

100

可以使用git的filter-branch命令做刪除有問題的文件,這樣一個偉大的教程:

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

你可以在這裏找到更多的文檔http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository

+1

請注意,它也會刪除當前簽出的版本,因此如果需要保留它,請確保您有備份。 – rleelr

+5

我正在使用Windows。我不得不將單引號換成雙引號。 –

+1

@rleelr我認爲github頁面給出了一個乾淨的解決方案,而不會影響其他文件:https://help.github.com/articles/removing-sensitive-data-from-a-repository/ – MeadowMuffins

13

您可以從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 
[email protected] 
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 
+0

請小心,因爲如果您有許多像我這樣的提交(大約30k,並且它會一整天),此腳本可以運行很長時間。 –