Git “Unlink of file” issue

This is a fairly common problem with Git, the Unlink of file issue. I encountered it on this occasion when attempting to run git gc to clean up and optimize my local repository.

There’s plenty of documentation around this issue, however I found a post that is directed at Windows users. Essentially the issue revolves around .pack files in the git/objects/pack directory, these files can be observed and git needs to delete them as part of the clean up operation. However an IDE Like Eclipse will hold these files in an open state which prevents Git from cleaning up, hence the error.

I found that simply closing Eclipse and Visual Studio Code before running git gc again, fixed the issue.

There’s more, this fix also cleared up an issue I was having with GitKraken not picking up local directories.

git gc
Enumerating objects: 10354, done.
Counting objects: 100% (10354/10354), done.
Delta compression using up to 8 threads
Compressing objects: 100% (4002/4002), done.
Writing objects: 100% (10354/10354), done.
Total 10354 (delta 5990), reused 10354 (delta 5990)
Unlink of file '.git/objects/pack/pack-418620f3800a92a829274bb73f304a43f4d476f5.pack' failed. Should I try again? (y/n)