Access denied to file on windows 7 for Administrator account

Gelin Luo picture Gelin Luo · Dec 29, 2010 · Viewed 20.9k times · Source

I want to delete my git repository (.git folder) created by git on my windows 7 box. It says access denied to .git/refs/header folder.

I open a console with Administrator role, issue command "takeown /F heads /A /R", it still says "ERROR: Access is denied.", neither can i do via GUI security dialog.

Any suggestion?

Regards, Green

Answer

Leo Davidson picture Leo Davidson · Dec 29, 2010

An access denied error may mean the file is in use by another process and, for a GIT repository, that seems more likely than a file permissions issue.

(I'm assuming GIT is like SVN where every file in the repository is created by your own account with standard permissions. If that's not the case then I may be wrong.)

  • As a first step, run Process Explorer as Administrator and push Ctrl-F to open a window where you can type the filename and see which processes (if any) have that file open.

    When I say "run as Administrator":

    I don't just mean when logged in as Administrator. Windows 7's UAC means Administrator accounts don't run things with full admin access by default. Right-click procmon.exe and choose the "Run as Administrator" option.

    I assume you're already familiar with this from running the command prompt as admin. If you just changed to an admin account, but didn't use "Run as Administrator" on the command prompt, then that could be why the takeown command failed. The command prompt should say "Administrator:" in its window title if it is properly elevated, unless UAC is off entirely.

  • If one or more processes are listed, confirm the full file path in the list (in case some other file on disk has a similar name).

  • Close any processes which have the file open. Try to close them normally if you can. As a last resort you can end-task them. If it's a process running as another user/account, it might be a service which you can stop.

    (Ensure View->Show Processes for All Users is ticked, then find the process in question via the PID column. If you think it's a service, hover the mouse over it and a tooltip will appear telling you the name(s) of any services it is running.)

  • If you cannot find any process accessing the file, or closing them all still results in an access denied error, and you are definitely trying the deletion from an elevated command prompt, then you might want to try telling Windows to delete the file the next time it is rebooted. You can use the SysInternals MoveFile tool for that.