How to remove dangling commits in remote git repository like VSTS

Viewed 4328

I've been trying to decrease the size of my Git repository by moving some files to git LFS. A few 100 commands further, here we are with files in git LFS and no more history of these files in my git commits.

However, whenever I clone the repository I am still downloading about 3gb of objects. I worked around this issue by creating a new repository in Visual Studio Team Services and after pruning and garbage collecting my repository locally, then doing a push to there, it was reduced to 300mb. (Command to do this locally found in this post: Git: what is a dangling commit/blob and where do they come from?)

However I can't imagine that you always have to delete / recreate a complete repository to remove dangling commits.

What I also tried was doing a git init, and then pushing that over the existing repository, but it only increased the object count further.

For anyone also running into similar issues, these were the command I executed to create a new repository without dangling commits, I would however like to find out how to do this in the existing repository without having to delete it:

git clone https://avavedse.visualstudio.com/Test/_git/TestRepository
cd blahblah
git reflog expire --expire=now --all
git gc --prune=now
git remote add newrepo https://avavedse.visualstudio.com/Test/_git/TestRepositoryNewEdition
git push newrepo
2 Answers

This may be a duplicate of How to remove a dangling commit from GitHub?

GitHub will periodically garbage collect objects that cannot be reached from a top-level reference. So over time, they'll disappear. But this is not guaranteed. That's the best info I've found on this.

You can manually correct reflogs expiry date to now and run garbage collector:

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

But this will only affect the local repo.

Apparently, the garbage collector is far from being ideal, insomuch that unless you don't mind deleting and creating a new repo and losing all issues, pull requests, etc., you have to contact Github Support :

you can permanently remove cached views and references to the sensitive data in pull requests on GitHub by contacting GitHub Support or GitHub Premium Support.[docs.github.com]

Related