Git Checkout warning: unable to unlink files, permission denied

I am aware that there are similar issues about git relating to the 'unable to unlink' warning, but I have not been able to use them.

The main difference is that this happened when I have not been dealing with submodules in any way (I have never dealt with them before). I created a branch called 'upgrade', deleted my old framework files, and copied in the new ones. I used git add -A, then committed everything. When I tried to checkout the trunk branch, it responded with the following errors:

warning: unable to unlink requirements/views/sk/index.php: Permission denied
warning: unable to unlink requirements/views/sv/index.php: Permission denied
warning: unable to unlink requirements/views/zh/index.php: Permission denied
warning: unable to unlink requirements/views/zh_cn/index.php: Permission denied
warning: unable to unlink requirements/views/zh_tw/index.php: Permission denied

...etc. There are hundreds of them.

At first I thought this was simply a permissions problem, and so I added group write permissions to the entire requirements directory recursively, but there was no change.

Edit: As suggested in an answer below, I attempted to do the same thing but with everything else closed. I had no more luck than before.

This issue is particularly debilitating as I cannot move to trunk to get back to normal development.


Solution 1:

In my first encounter with this error my user had rights to "write" to the file but not the containing directory. Check the permissions of the directory containing the file.

Solution 2:

I usually see that kind of error when there is a process not releasing the handle of those files.

Make sure nothing is running, and then try your checkout again.

Note: it can also be related with the way Git has been installed (on Windows, UAC can generate problem if msysgit is installed in C:\Program or C:\Program Files, see "msysgit - sh.exe - fork: Permission denied - Vista 64 bit" and comment 2 of issue 437)

Note: as illustrated below, a common other cause of the problem is rights issue on the directory (wrong owner), not necessarily on the file that can't be unlinked.

Solution 3:

"Unlink" essentially means "delete file" in this case.

This error is not caused by git itself. You should have similar errors deleting those files manually, in a command line or file explorer.

Solution 4:

You don't have the access permission, maybe because you are not the owner.

Fix by changing the owner to yourself:

sudo chown -R your_login_name /path/to/folder