112

I accidentally added, committed and pushed a huge binary file with my very latest commit to a Git repository.

How can I make Git remove the object(s) that was/were created for that commit so my .git directory shrinks to a sane size again?

Edit: Thanks for your answers; I tried several solutions. None worked. For example the one from GitHub removed the files from the history, but the .git directory size hasn't decreased:

$ BADFILES=$(find test_data -type f -exec echo -n "'{}' " \;)

$ git filter-branch --index-filter "git rm -rf --cached --ignore-unmatch $BADFILES" HEAD
Rewrite 14ed3f41474f0a2f624a440e5a106c2768edb67b (66/66)
rm 'test_data/images/001.jpg'
[...snip...]
rm 'test_data/images/281.jpg'
Ref 'refs/heads/master' was rewritten

$ git log -p # looks nice

$ rm -rf .git/refs/original/
$ git reflog expire --all
$ git gc --aggressive --prune
Counting objects: 625, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (598/598), done.
Writing objects: 100% (625/625), done.
Total 625 (delta 351), reused 0 (delta 0)

$ du -hs .git
174M    .git
$ # still 175 MB :-(
7

9 Answers 9

157

I answered this elsewhere, and will copy here since I'm proud of it!

... and without further ado, may I present to you this useful script, git-gc-all, guaranteed to remove all your git garbage until they might come up with extra config variables:

git -c gc.reflogExpire=0 -c gc.reflogExpireUnreachable=0 \
  -c gc.rerereresolved=0 -c gc.rerereunresolved=0 \
  -c gc.pruneExpire=now gc "$@"

The --aggressive option might be helpful.

NOTE: this will remove ALL unreferenced thingies, so don't come crying to me if you decide later that you wanted to keep some of them!

You might also need to run something like these first, oh dear, git is complicated!!

git remote rm origin
rm -rf .git/refs/original/ .git/refs/remotes/ .git/*_HEAD .git/logs/
git for-each-ref --format="%(refname)" refs/original/ |
  xargs -n1 --no-run-if-empty git update-ref -d

I put all this in a script, here:

https://ucm.dev/t/bin.git/git-gc-all-ferocious

11
  • 2
    As in stackoverflow.com/questions/1904860/…, +1 to you again.
    – VonC
    Feb 6, 2013 at 17:57
  • 24
    excellent :D my evil plan to get more points by cloning answers has worked!!1 ;) Feb 8, 2013 at 2:39
  • 5
    102m to 160k.. effective and destructive
    – prusswan
    Mar 15, 2016 at 3:30
  • 5
    Thanks so much for the script! Bonus info: The xargs command produces an error on OS X because of an unrecognized option. Simplest solution: Install GNU xargs via homebrew brew install findutils and replace xargs by gxargs.
    – qqilihq
    Sep 27, 2017 at 20:21
  • 1
    @SamWatkins The URL you added sam.nipl.net/b/git-gc-all-ferocious is now old, outdated & 404, can you update the URL so it doesn't go 404 when someone looking for assist clicks on it ?
    – Vicky Dev
    Aug 10, 2022 at 13:25
31

Your git reflog expire --all is incorrect. It removes reflog entries that are older than the expire time, which defaults to 90 days. Use git reflog expire --all --expire=now.

My answer to a similar question deals with the problem of really scrubbing unused objects from a repository.

25

1) Remove the file from the git repo (& not the filesystem) :

  • git rm --cached path/to/file

2) Shrink the repo using:

  • git gc,

  • or git gc --aggressive

  • or git prune

or a combination of the above as suggested in this question: Reduce git repository size

0
10

This guide on removing sensitive data can apply, using the same method. You'll be rewriting history to remove that file from every revision it was present in. This is destructive and will cause repo conflicts with any other checkouts, so warn any collaborators first.

If you want to keep the binary available in the repo for other people, then there's no real way to do what you want. It's pretty much all or none.

9

The key for me turned out to be running git repack -A -d -f and then git gc to reduce the size of the single git pack I had.

1
  • Yes, thank you! This one actually worked, unlike everything else. Aug 17, 2023 at 3:07
7

Hy!

Git only receives objects it actually needs when cloning repositories (if I understand it correctly)

So you can amend the last commit removing the file added by mistake, then push your changes to the remote repository (with -f option to overwrite the old commit on the server too)

Then when you make a new clone of that repo, it's .git directory should be as small as before the big file(s) committed.

Optionally if you want to remove the unnecessary files from the server too, you can delete the repository on the server and push your newly cloned copy (that has the full history)

5
git filter-branch --index-filter 'git rm --cached --ignore-unmatch Filename' --prune-empty -- --all

Remember to change Filename for the one you want to remove from the repository.

5

See "Removing Objects" in the Pro Git book:

http://git-scm.com/book/en/Git-Internals-Maintenance-and-Data-Recovery#Removing-Objects

Update: see also BFG repo cleaner: http://rtyley.github.io/bfg-repo-cleaner/

0

In 2020 the documentation for git-filter-branch discourages its use and recommends using an alternative such as git-filter-repo. It can also be used instead of BFG.

Note that the chapter on Rewriting History in the git book hasn't been updated. Neither has GitHub's recommendation on removing sensitive data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.