git use up space even when file is unstaged

Viewed 85

I wanted to use git-lfs to add a large binary into my git repository but that failed:

$ git add large-file.tar.xz

So I unstage the file

$ git checkout -- large-file.tar.xz
$ git status
On branch lfs
nothing to commit, working tree clean

But the git still use up the disk space for the large file.

$ du -sh .git
101M    .git

Reclaiming the space

I tried using the BFG repo cleaner to remove the blob, but nothing is found. (Yes, BFG only scan for existing commits, but I was desperate)

$ bfg --strip-blobs-bigger-than 10M .

Using repo :path/to/project

Scanning packfile for large blobs: 725
Scanning packfile for large blobs completed in 31 ms.
Warning : no large blobs matching criteria found in packfiles - does the repo need to be packed?

git gc does not work as well

$git reflog expire --expire=now --all && git gc --prune=now --aggressive
$ du -sh .git
101M    .git
3 Answers

Don't forget to delete the original objects left as a backup by any bfg or filter-branch operation.

rm -Rf .git/refs/original

Then you can prune the rest:

git reflog expire --expire=now --all
git gc --prune=now
git gc --aggressive --prune=now
git repack -Ad      # kills in-pack garbage
git prune           # kills loose garbage

Use below commands. It will remove loose objects from .git

git reflog expire --expire=now --all 
git repack -ad  # will remove loose objects from compressed objects
git prune       # remove loose objects

enter image description here

Checking out a file doesn't unstage it. Use:

git reset HEAD large-file.tar.xz
git prune
Related