136

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.

2

29 Answers 29

118

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.

3
  • 123
    Oh my, too funny, I just came across this answer today and then I realized it was my own! Nonetheless, it worked again! Aug 11, 2014 at 16:51
  • Ok, it happend to me on windows 10, i go to the main project folder. and add FOR ALL POSSIBLE USERS, all permissions. So for system, administator, users, all possibilities. Apply chnages. And it seems to work, somehow maybe with windows 10 update, even if we dont create new user, we are threated like the new one, without permissions. For example i have some wird name S-1-15-32..... Not the login name that i have when we unlock our laptop.
    – Jakub S.
    Jun 19, 2019 at 9:42
  • Nice! I just came across this again here in 2023, not from searching, but I remembered it in my head. In this case it was for an Ansible playbook and the file module and the 'absent' argument and I was getting FAILED! => {"changed": false, "msg": "rmtree failed: [Errno 13] Permission denied: '/opt/tmp/folder-name'"} and the owner of /opt/tmp was incorrect. Sep 27 at 23:34
101

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.

9
  • 1
    I'm on Ubuntu, to clear that up. And unfortunately I'm only in this web browser and my console, which has one tab open to the relevant directory (and two open quite elsewhere).
    – Paragon
    Aug 26, 2011 at 4:11
  • 4
    @Paragon: even on unix, you can have handle issues. Otherwise, this should be a permission issue of some kind. You should be able to force the checkout though. git checkout -f master
    – VonC
    Aug 26, 2011 at 5:37
  • 2
    +1 in my case it was the folder sharing with an active VM which prohibited discard of files in git on the host system. Drove me nuts, so thanks for the hint!
    – Jook
    Jul 2, 2013 at 10:56
  • 1
    Same issue. Running Process Explorer > Ctrl+F > <filename> - will display process, which keep this file opened.
    – setevoy
    Jul 22, 2015 at 14:39
  • 1
    GitExtensions displayed this error when attempting to fetch all... I also had GitKraken open. Once I closed GitKraken the fetch all worked without error.
    – mkaj
    Feb 2, 2017 at 1:08
35

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

3
  • 23
    In my first encounter with this error my user had rights to "write" to the file but the containing directory did not. Dec 26, 2012 at 17:18
  • 3
    @Elijah: Thank You! That's what it was for me.
    – Jesse Lee
    Feb 27, 2013 at 16:13
  • 4
    In my case I found the file in question was locked by another application. Closing the application freed the file and allowed the checkout to proceed.
    – Simon Elms
    Mar 1, 2013 at 0:07
35

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
1
  • 2
    On my local development machine, the files in question had been originally created by my local apache server, so were owned by the www-data user. Once I chown'd them to my own account, everything worked normally again. "Permission Denied" was the real issue. The "unable to unlik" was just a red herring. Feb 24, 2015 at 21:31
22

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.

1
  • 4
    While you might not want a 777 on any folder. This did solve my problem, but I quickly changed it back to default after it was solved. Thanks!
    – Bram
    Mar 24, 2016 at 20:53
13

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.

0
8

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.

6

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: http://hints.macworld.com/comment.php?mode=view&cid=1734

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.

1
4

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.

1
  • That was it. Happened with Android Development, as AndroidStudio is Intellij.
    – Reinherd
    Dec 16, 2013 at 10:57
3

on terminal on mac i just do this

sudo git checkout . ( to clean up everything )

and then

sudo git pull origin

2

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

2

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.

2

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.

1

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

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

0

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!

0

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.

0

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.

1
  • You can actually disable the status cache of TortoiseGit completely, and it's generally something that I would recommend. It's often the cause of a lot of unexpected file locks, and it tends to be much more trouble then it's worth. Just use git status from the command line instead.
    – user456814
    Jul 11, 2014 at 16:20
0

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

2
  • and what about the windows users? Jul 27, 2015 at 12:06
  • The OP was on Ubuntu. I wouldn't have commented on a Windows thread.
    – Marc
    Jul 28, 2015 at 20:02
0

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

0

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.

0

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.

0

git gc worked for me (in a new tab). Was getting this with every rebase. Thanks http://www.saintsatplay.com/blog/2016/02/dealing-with-git-unlink-file-errors#.W4WWNZMzZZJ

0

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

    chmod ug+w <directory path>
0

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
0

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

chown -R www-data project_folder_name
0

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.

0

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.

0

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.

0

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.