2011-11-10 20 views
55

Possible Duplicate:
How to purge a huge file from commits history in Git?¿Cómo elimino un archivo grande cometido erróneamente en git

hice una cosa estúpida. Imagine que he cometido un archivo de 100 MB. Luego veo esto y borro este archivo y vuelvo a comprometerme. Este es un procedimiento normal para eliminar un archivo.

Pero ahora el efecto secundario es que mi historial es pesado porque se ha guardado este archivo grande (creo que esta es la razón por la que es pesado). Solo estoy usando git local, así que no sincronizo en ningún servidor.

¿Cómo puedo eliminar definitivamente este archivo y ahorrar espacio en el disco?

+1

Vea la respuesta aceptada para mi pregunta http://stackoverflow.com/questions/796983 1/drop-old-commit-git-rebase-causes-merge-conflicts –

+0

Usa el repo-cleaner BFG, una alternativa más simple y más rápida a 'git-filter-branch' específicamente creado por mí para eliminar los archivos no deseados del historial de Git. Consulte http://stackoverflow.com/a/17890278/438886 –

Respuesta

100

usted puede hacerlo usando el comando git filter-branch, así:

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

Puede encuentre más documentación aquí http://dalibornasevic.com/posts/2-permanently-remove-files-and-folders-from-a-git-repository

+1

Tenga en cuenta que también eliminará la versión actualmente desprotegida, así que asegúrese de tener una copia de seguridad si la necesita. – rleelr

+5

Estoy usando Windows. Tuve que cambiar las comillas simples por comillas dobles. –

+1

@rleelr Creo que la página de github da una solución limpia sin afectar los otros archivos: https://help.github.com/articles/removing-sensitive-data-from-a-repository/ – MeadowMuffins

13

Usted puede tomar este gran guión de David Underhill para eliminar el archivo desde el repositorio 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

Tenga cuidado ya que este script puede ejecutarse durante mucho tiempo si tiene muchos commits como yo (~ 30k y se irá todo el día). –

Cuestiones relacionadas