12

How can I remove a folder that is already deleted from the git history?

In my git repository I have a folder called /foo (1.2GB of size). I have deleted the folder foo with rm -rf /foo beause I do not need it any more. After many other commits I thought. Why is my remote repo so big...I have forgotten to do git rm --cached ... instead of rm -rf .... How can I now remove the folder from git history?

git rm --cached /foo won't work because the folder is already delete in an earlier commit.

aheze
  • 24,434
  • 8
  • 68
  • 125
Marten Bauer
  • 3,099
  • 5
  • 22
  • 18
  • 1
    Check this question, which addresses similar issue: http://stackoverflow.com/questions/6358476/how-to-remove-old-versions-of-media-files-from-a-git-repository – sateesh Jan 20 '14 at 09:23
  • Also, take a look here http://stackoverflow.com/questions/2100907/how-do-i-purge-a-huge-file-from-commits-in-git-history and here http://dound.com/2009/04/git-forever-remove-files-or-folders-from-history/ ; Generally any googling about "git large file delete permanent" comes up with something about "filter-branch" utility. Never used it myself though. – quetzalcoatl Jan 20 '14 at 12:44

1 Answers1

30

The documentation covers the similar case of purging a file from history:

git filter-branch --index-filter 'git rm --cached --ignore-unmatch filename' HEAD

Since you are deleting a whole directory, add the -r flag to git rm:

git filter-branch --index-filter \
                  'git rm -r --cached --ignore-unmatch path/to/directory' HEAD

Note that this operation will take several minutes on larger repositories.

More importantly, it will make a new repository with distinct history and checksums. If you previously published your repository, the history of the new one will not be compatible with the history others have pulled.

user4815162342
  • 141,790
  • 18
  • 296
  • 355
  • 18
    Your `filter-branch` command didn't work for me. I found one [here](http://stevelorek.com/how-to-shrink-a-git-repository.html) that does: `git filter-branch --tag-name-filter cat --index-filter 'git rm -r --cached --ignore-unmatch filename' --prune-empty -f -- --all ` – Stephen Ostermiller Oct 07 '14 at 13:19
  • 1
    @StephenOstermiller's comment should be the answer... [the link he shared](http://stevelorek.com/how-to-shrink-a-git-repository.html) was amazing. – kevnk Sep 16 '15 at 16:19
  • 1
    The link that @StephenOstermiller shared is not longer available , I found it in wayback machine: https://web.archive.org/web/20190106011431/http://stevelorek.com/how-to-shrink-a-git-repository.html – Diego Ricardo Valdivia Arreola Sep 21 '21 at 23:50