1

I created a branch from the master, and have been adding and deleting a large number of binaries into the branch, As a result, the repo size has bloated to thrice its original size. I have removed the binaries and deleted the branch but the repo size does not change. How can i minimize the repo size?

Aamir Shaikh
  • 345
  • 1
  • 2
  • 13
  • https://git-scm.com/docs/git-gc - but use caution! – quadroid Sep 05 '18 at 10:23
  • The repo size may not change much because those binary files are still present in the history of the branch(es). You have to go back and remove those binary files somehow. The duplicate link offers one option. – Tim Biegeleisen Sep 05 '18 at 10:25
  • @Console That's not what this question is about. `git-gc` won't work in the case of binary files which are still being referred to by active commits, in active branches. – Tim Biegeleisen Sep 05 '18 at 10:26
  • @TimBiegeleisen he deleted the branch therefore no active commits reference the binaries (as I understood the question). – quadroid Sep 05 '18 at 10:28
  • @Console You might be right, but that would mean that this branch never interacted with anything else. I have removed the duplicate mark, but it might have been appropriate. – Tim Biegeleisen Sep 05 '18 at 10:29
  • @Console I need to be 100% sure whatever cleanup I do does not affect master or any other branch – Aamir Shaikh Sep 05 '18 at 10:58

1 Answers1

2

If you delete a Branch in git only the named ref is removed, the commits which store the source are still around and can viewed with git reflog or restored (as described here). Therefore deleting a branch does not free any diskspace (out of a view kb's). This commits are called dangling commits because there is no branch / tag that references the objects. To make git clean up this stuff call

git reflog expire --expire-unreachable=now --all
git gc --prune=now

This does of course only work if you have no branch referencing the commits containing the binary. And this does aswell destroy the reflog - making it impossible to restore currently deleted commits and branches.

Take a look at this Answer https://stackoverflow.com/a/4528593/2250672

quadroid
  • 8,444
  • 6
  • 49
  • 82
  • 1
    This is mostly right (& upvoted), except: *This objects are called loose objects because there is no branch / tag that references the objects.* That's not why they're called "loose". :-) Loose just means "not yet packed". In modern Git you don't need to care about loose vs packed, just run `git gc` as you show here. Meanwhile: `git reflog expire --expire-unreachable=now --all` - the only reflog you need to expire-unreachable from is that of `HEAD`, so you can trim this down a bit, but your caveat is correct, it's removing your safety belts. – torek Sep 05 '18 at 18:29
  • @torek I may have mixed dangling and loose - I changed the wording to `dangling commits`. – quadroid Sep 06 '18 at 06:36