copy/mv directory to another directory in git

28,073

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
28,073
Kyaw Siesein
Author by

Kyaw Siesein

Software Engineer, rails, ruby, django,web2py,puppet, python,linux

Updated on July 09, 2022

Comments

  • Kyaw Siesein
    Kyaw Siesein almost 2 years

    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?

  • Vince
    Vince almost 10 years
    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
    Vince almost 10 years
    I'm pretty sure you'll lose the modification history of the files you move independently of Git.
  • user518450
    user518450 almost 10 years
    @Ghodmode: no, git is content based and does rename detection based on file content.
  • Vivian De Smedt
    Vivian De Smedt over 9 years
    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.
  • ingomueller.net
    ingomueller.net about 9 years
    This solved my problem as well! BTW, I usually use unlocker to unlock files locked by a process.
  • Olumide
    Olumide over 8 years
    I just killed the explorer process and restarted it and voila!