41

I would like to maintain history of files within a sub-folder of a repo. But I need to rename the top level folder name. When I run 'git mv dirName newDirName I get 'Fatal: source directory is empty;'

My source directory has the following structure:

gitRepoDir
--.git
--Source 
--  -DirLevel2 
--   --DirLevel3 
--     --DirLevel4 
--       --DirLevel5 
+++       --DirNameToRename 
+++         --sub1dir 
+++           --File 
+++         --sub2dir 
+++           --File 
+++         --sub3dir 
+++           --File 
+++           --File 
+++         --sub4dir 
+++           --File

Is there a way to rename the top level folder and maintain history of the files in the below subfolders? Or do I need to create the directory structure first then move the files with the git mv command?

koderiter
  • 441
  • 1
  • 4
  • 9
  • where is your .git folder? – gzh Mar 10 '16 at 02:17
  • See above edit of directory structure – koderiter Mar 10 '16 at 14:42
  • 1
    Was this in Windows? The git commands are still case sensitive in Windows. The error is suggesting it cannot find the source folder (or that it can and is empty). Ensure the casing used in the command matches that of the directory in Windows explorer. – wnbates Aug 27 '17 at 07:08

6 Answers6

53

I think it, 'Fatal: source directory is empty;', means dirName is not under git control because it's not part of source. For example, you can try "git status", to figure out whatever can be controlled by git.

Freedom
  • 630
  • 4
  • 9
  • 7
    The command to show files which is managed by git is `git ls-files`. – gzh Mar 10 '16 at 04:16
  • I perform `git ls-files` returns nothing until DirlLevel2 then it lists files under git control. all the files that I need moved are listed. I tried to execute from DirLevel2 `git mv dirlevel3/dirlevel4/dirlevel5/DirNameToRename ...` and I get the same result. – koderiter Mar 10 '16 at 14:45
  • 1
    yup, those files are not under git control, thanks! :-) – CeDeROM Apr 09 '18 at 23:19
16

you should just tell git mv to skip any operation leading to failure condition

git mv -k dirName newDirName
Mirco Ellmann
  • 983
  • 7
  • 8
8

Usually, the pathname used in git command is a relative path started from current directory.

So for your question, followings should be checked.

1) Whether dirName is a directory managed by git. you can use command git ls-files

2) Whether dirName folder is under current director.

gzh
  • 3,507
  • 2
  • 19
  • 23
  • 1
    `git ls-files` returns nothing in the current directory that I'm executing from. DirLevel5 in this case (see above directory structure) at DirLevel2 is where `git ls-files`and it does list the files. – koderiter Mar 10 '16 at 14:52
0

In my case is really what that error message source directory is empty indicates, i.e. empty source directories can't be commit unless I added .gitignore file as dummy file in that directories. Only after that I can git mv that directories.

林果皞
  • 7,539
  • 3
  • 55
  • 70
0

This error can also happen due to case sensitivity. Even on Windows. Eg:

git mv source/dirlevel2/ otherdir/
Fatal: source directory is empty
Fix:
git mv Source/DirLevel2/ OtherDir/
Robert
  • 78
  • 6
-3

This can happen on Windows if you use backslash in the path for the source folder.

NateS
  • 5,751
  • 4
  • 49
  • 59