Git Checkout warning: unable to unlink files, permission denied


Git Problem Overview

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.

Git Solutions

Solution 1 - Git

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 - Git

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 - Git

"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 - Git

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

Solution 5 - Git

I was having the issue with a default-settings.php file in drupal 7. In this case I wasn't able to delete it or revert it just like @rtconner said. I didn't have an application or anything using this file, and it ended up being a permissions error.

I added chmod 777 * to the folder and then I was able to revert it no problem.

Solution 6 - Git

You can change the write permissions to get it done.

sudo chmod -R ug+w . 

This command will give 'w' permissions to all the folders in the current directory.

Solution 7 - Git

I ran into this problem whenever running "git repack" or "git gc" on my OS X machines, even when running git with admin privileges, and I finally solved it after coming across this page:

The fix is to open a terminal, go to your git repo, cd into the .git folder, and then do:

chflags -R nouchg *

If that was the issue, then after that, your git commands will work as normal.

Solution 8 - Git

This can also occur when:

  1. You ran a process inside a Docker container, and:

  2. Some files were generated by that process, and:

  3. The destination of the files is mounted as a volume on the Docker host, and:

  4. You are running git on the Docker host.

If this is the case, stage the files you wish to commit and run:

git diff --name-only --cached | xargs ls -l 

Files which meet the above criteria will be prefixed with:

-rw-r--r-- 1 root root ...

They are owned by root and not writable, which is not good. To fix that run:

 git diff --name-only --cached | xargs -i sh -c 'sudo chown $USER:$USER {}; chmod +w {}'

A cleaner solution would probably be to use the --user option, see this for Docker and this for Docker compose.

Solution 9 - Git

To those who are using Intellij, as @rtconner said this problem is not caused by git. Since your IDE is locked a file(s) git cannot un-link it. So, you need to close your IDE and then try to merge (or whatever you like) it via command line.

Solution 10 - Git

on terminal on mac i just do this

> sudo git checkout . ( to clean up everything )

and then > sudo git pull origin

Solution 11 - Git

In my case it was a ":" character in a folder name prevenitng the git repo to checkout on windows.

Solution 12 - Git

I had this error inside a virtual machine (running Ubuntu), when I tried to do git reset --hard.

The fix was simply to run git reset --hard from the OS X host machine instead.

Solution 13 - Git

In my case, my Windows directory is under the Dropbox folder. It is not a Git-specific issue. When a file (in this case, the lock file) has just been created, it takes Dropbox another second to do the synchronization. During that time, the file is in use by Dropbox, and any 3rd party program (in this case, Git) cannot delete the file.

My solution is to quit Dropbox and therefore avoiding the behind-the-scenes magic of Dropbox's file synch.

Solution 14 - Git

None of the other suggestions worked for me, but this did:

sudo git reflog expire --expire=now --all && sudo git gc --prune=now --aggressive

Solution 15 - Git

I have encountered this error and it is caused by wrong "owner/group" of the file/folder. You must seek assistance to your server admin to change the "owner/group" of this file/folder and retry using "git pull" again. Or if you are a sudoer, just sudo chown "your owner name / your group name" and try again pulling your repository. Try it, it works 100% to me!

Solution 16 - Git

Make sure that any associated processes or threads are not running and perform end task or force quit as necessary.

Make sure you change the ownership permission.

Solution 17 - Git

Just in general, if this happens in Windows, and you are using tortoisegit, it is the status cache of tortoisegit. Kill that process and it will be freed.

Solution 18 - Git

I just had to switch user from ubuntu to my actual user name that I'd first done stuff under. That fixed it.

Solution 19 - Git

Solved for me by setting my git client (GitExtensions) to always run in admin mode.

Solution 20 - Git

I had that problem while using IntelliJ (14.1.3 Ultimate), I wanted to revert changes in some file.

Solved by closing Git Bash opened in another window - another revert trial in IntelliJ worked.

Solution 21 - Git

I encountered this error and I think the issue was that I had 'run as admin' when I started Eclipse and created the files, therefore they were owned by Admin (noticed by running 'ls -la' on the folder). When I later tried to stash the files, it didn't let me ('unable to unlink files' and all that). Doing a chmod on the files was the fix for me.

Solution 22 - Git

git gc worked for me (in a new tab). Was getting this with every rebase. Thanks

Solution 23 - Git

All you need to do is provide permissions, run the command below from the root of your project:

    chmod ug+w <directory path>

Solution 24 - Git

I had the same issue , I tried few alternatives as others suggested.

But finally giving correct permission to .git folder solve the issues.

sudo chown -R "${USER:-$(id -un)}" .git

Solution 25 - Git

In my case the permission problem solved by setting www-data as an owner:

chown -R www-data project_folder_name

Solution 26 - Git

I've encountered this error while running Git Bash on a windows box. In my particular case I just needed to open Git Bash as administrator.

Solution 27 - Git

For me, at the top of the message there was:

Auto packing the repository in background for optimum performance.
See "git help gc" for manual housekeeping.
error: The last gc run reported the following. Please correct the root cause
and remove .git/gc.log.
Automatic cleanup will not be performed until the file is removed.

So I removed the file with rm .git/gc.log, and then I got:

Auto packing the repository in background for optimum performance.
See "git help gc" for manual housekeeping.
error: The last gc run reported the following. Please correct the root cause
and remove .git/gc.log.
Automatic cleanup will not be performed until the file is removed.

warning: There are too many unreachable loose objects; run 'git prune' to remove them.

After running git prune, everything was back to normal.

Solution 28 - Git

I solved this problem, just by delete all temp data > Run>%temp% >Hit enter and delete all temp files and restarting the computer. For me it is working now.

Solution 29 - Git

I had the same problem on Windows. Fixed it by using the following command instead:

git show feature-branch:./filePath > ./filePath


All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionParagonView Question on Stackoverflow
Solution 1 - GitElijah LynnView Answer on Stackoverflow
Solution 2 - GitVonCView Answer on Stackoverflow
Solution 3 - GitrtconnerView Answer on Stackoverflow
Solution 4 - GitVince YuanView Answer on Stackoverflow
Solution 5 - GitRob ErskineView Answer on Stackoverflow
Solution 6 - GitRajendra kumar VankadariView Answer on Stackoverflow
Solution 7 - GitLearn OpenGL ESView Answer on Stackoverflow
Solution 8 - GitdavetapleyView Answer on Stackoverflow
Solution 9 - GitHesamView Answer on Stackoverflow
Solution 10 - GitJryView Answer on Stackoverflow
Solution 11 - GitsimonCView Answer on Stackoverflow
Solution 12 - GitHenrik NView Answer on Stackoverflow
Solution 13 - GitVincent YinView Answer on Stackoverflow
Solution 14 - Git0x1masonView Answer on Stackoverflow
Solution 15 - GitJeff Simons DecenaView Answer on Stackoverflow
Solution 16 - GitCreative_CimmonsView Answer on Stackoverflow
Solution 17 - GitWiseStrawberryView Answer on Stackoverflow
Solution 18 - GitMarcView Answer on Stackoverflow
Solution 19 - Gitfrodo2975View Answer on Stackoverflow
Solution 20 - GitEel LeeView Answer on Stackoverflow
Solution 21 - GitLConradView Answer on Stackoverflow
Solution 22 - GitmadavView Answer on Stackoverflow
Solution 23 - GitHansaView Answer on Stackoverflow
Solution 24 - GitRaza RafaideenView Answer on Stackoverflow
Solution 25 - GitAvag SargsyanView Answer on Stackoverflow
Solution 26 - GitCraigView Answer on Stackoverflow
Solution 27 - Gituser2340939View Answer on Stackoverflow
Solution 28 - GitMuhammad BilalView Answer on Stackoverflow
Solution 29 - GitBonnevView Answer on Stackoverflow