21

I have a

dir1/ contains a lot files and subdirecties, I want to change the directory name to dir2/ . I tried

git mv dir1/ dir2/

I got this message:

fatal: renaming 'dir1' failed: Permission denied

update:

I ran from git for windows command line http://code.google.com/p/msysgit/
What is the correct command?

2
  • What operating system are you using for executing these commands?
    – dwmcc
    Jul 28, 2011 at 23:09
  • I ran from git for windows command line code.google.com/p/msysgit
    – icn
    Jul 28, 2011 at 23:11

5 Answers 5

20

It could also be that the directory (or a file within) is being used by another program, which prevents you from doing anything with that folder. Only on Windows, obviously.

Use the Process Explorer if you're unsure which program has captured that directory/file.

4
  • This was exactly the cause of the problem for me. I think this is a better answer than the accepted answer because moving the directory independently of Git will cause you to lose modification history of any of the files in the directory you're moving. Besides that, won't it fail anyway if the OS has files in the directory locked?
    – Vince
    May 30, 2014 at 20:10
  • It solved my problem too. TGitCache.exe was the process to hold the folder. I suspect TGitCache will hold every folder under git control making the rename command not working nearly 100% of time. Jan 2, 2015 at 23:29
  • This solved my problem as well! BTW, I usually use unlocker to unlock files locked by a process. May 18, 2015 at 10:47
  • I just killed the explorer process and restarted it and voila!
    – Olumide
    Jan 27, 2016 at 12:42
10

You can just use standard unix tools, or whatever your OS is. So:

mv dir1 dir2

should work. Just make sure you add both dir1 and dir2 to the staging area after you've done that, so that you commit the changes.

An example of how to commit the change (once done) could be:

git add dir1 dir2 && git commit dir1 dir2

There's probably other ways to do it too.

2
  • 4
    I'm pretty sure you'll lose the modification history of the files you move independently of Git.
    – Vince
    May 30, 2014 at 20:11
  • 3
    @Ghodmode: no, git is content based and does rename detection based on file content.
    – user518450
    Jul 2, 2014 at 18:58
2

Both the source and target directory need to be checked into the git repository. If you are moving dir1 to dir2 and dir2 hasn't been committed yet, you will get this error message.

0

I'm running git in cygwin. I had the same problem until I removed untracked files from the directory to be renamed. That allowed the git mv to complete properly.

If you see untracked files in your directory to be renamed when you run git status then you'll have to move those files somewhere else temporarily and bring them back in after the git mv.

0

I had the same problem. TGitCache.exe was the process that were preventing the folder to be renamed. Using TaksExplorer I have killed the process and run the command from Git Bash command prompt.

Your Answer

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

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