2

To remove a folder named build from git I use:

git rm --cached -r build

As I learned from this answer.

However, IIUC, the directory remains in the git's database.

I tested the matter:

  1. Initialised a new git repository.
  2. Added a new directory, named build, with a large file (7MB) and committed.
  3. performed:

    git rm --cached -r build
    

The size of the .git directory remained 7mb which means the untracking and deletion did not change the git history, presumptuously as it should.

What is the way to completely clean up a wrongly committed directory which was committed a few commits back?

In my case as I was only on my third commit with little concern to the history of the project, I deleted the .git folder and started over but I am wondering whether there is a better way.

Community
  • 1
  • 1
Guy
  • 12,488
  • 16
  • 79
  • 119

3 Answers3

3

The answer is the same as for removing a single file from all of history (with all the same caveats), except that instead of directing the filter to remove one file, you direct it to remove all files starting with that path, e.g., git rm --cached -rf path/to/directory as your --index-filter with git filter-branch. See, e.g., this answer to How to remove/delete a large file from commit history in Git repository? See also Completely remove file from all Git repository commit history.

Note that after using git filter-branch (which you should do on a clone), the simplest way to shrink the repository—which now has up to twice as many commits, since filters cannot alter existing commits, only copy them to corrected new ones—is to clone the filtered clone.

Community
  • 1
  • 1
torek
  • 448,244
  • 59
  • 642
  • 775
2
git filter-branch --tree-filter 'rm -rf src/folder' HEAD

git rm only removes from the working tree

rafalf
  • 425
  • 7
  • 16
1

Normally, you need to rewrite the history of commits in order to clean the large file from said commits.

One good tool is was BFG Repo cleaner: it is faster than the native git filter-branch

java -jar bfg.jar --strip-blobs-bigger-than 5M some-big-repo.git

Once you have filtered your commits, you still need to apply git gc/repack/prune in order for the size to actually go down.


Now (2020+), you would use git filter-repo as I did here:

git filter-repo --strip-blobs-bigger-than 5M
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250