991

I think it should work to copy the directory to be renamed to a new directory with desired name, and delete the old directory, and git add, git commit and push everything. But is this the best way?

Mus
  • 7,290
  • 24
  • 86
  • 130
qazwsx
  • 25,536
  • 30
  • 72
  • 106
  • 1
    Possible duplicate: [How to tell Git that it's the same directory, just a different name](http://stackoverflow.com/questions/6628539/how-to-tell-git-that-its-the-same-directory-just-a-different-name) – Christopher Peisert Jun 25 '12 at 05:35
  • 6
    As far as Git is concerned, a copy and delete is the same thing as a move. Git will record both (copy + delete) and (move) the same way. – Dietrich Epp Jun 25 '12 at 06:02

13 Answers13

1878

Basic rename (or move):

git mv <old name> <new name>

Case sensitive rename—eg. from casesensitive to CaseSensitive—you must use a two step:

git mv casesensitive tmp
git mv tmp CaseSensitive

(More about case sensitivity in Git…)

…followed by commit and push would be the simplest way to rename a directory in a git repo.

Community
  • 1
  • 1
CB Bailey
  • 755,051
  • 104
  • 632
  • 656
  • 7
    Does it save all the log and statistics? – orezvani May 26 '14 at 02:13
  • 45
    @ViliusK if you are dealing with case sensitive directories any easy way i've found is `git rm -rf --cached path/to/your/directories` then re-add and commit – dtothefp Nov 20 '14 at 22:09
  • in my case, from gitbash, i was trying to rename a folder that was not part of a git repository, so in that case simply calling this worked mv – BraveNewMath Sep 03 '15 at 17:31
  • 6
    But why GIT doesnt have a proper support for name change for a package/directory? Why do I even need to create a separate folder. When i change a name of the package shouldnt it take it as a difference and take care of it at the time of commit & push? – Ahmed Feb 23 '16 at 19:58
  • if you have hidden or excluded files that require `-force` to be added to the original repo - they will be skipped using this technique. be sure to check that the file counts match! – SliverNinja - MSFT Oct 18 '17 at 16:42
  • 1
    On windows I had to open `git bash` as Admin to make it work. – Prashanth Aug 19 '18 at 22:01
  • 1
    If you want to rename only case, just use `git mv -f casesensitive CaseSensitive` – Finesse Aug 28 '18 at 02:43
  • 5
    Thanks a lot for this. For me, I had to first execute `git config core.ignorecase false` and then run the commands in succession or else, for the second part I'd get a `source is empty` error. – Hossein May 18 '20 at 04:32
  • 2
    On Windows 10, when renaming a directory, `git mv` would complain: `Rename from [x] to [y] failed. Should I try again? (y/n)` even with Administrator privileges. The rename only succeeded from the console when I closed a File Explorer window that was viewing the parent directory which contained the directory I was renaming. – arcain Jun 25 '21 at 16:45
  • Do you also have to use mv old_folder_name new_folder_name? I changed the folder name and then tried changing the git name but got a fatal: bad source, source error. – Nick Aug 13 '21 at 17:21
  • Similar to @arcain, I had a file open in an editor that locked the file. Double check your open applications (and terminal windows?) when you get `Rename from 'scripts' to 'code' failed. Should I try again? (y/n)` on Windows 10. – user1556435 Nov 05 '21 at 14:42
  • excuse me but this doesn't work, get help page says "In the second form, the last argument has to be an existing directory; the given sources will be moved into this directory.", and this is exactly what it's trying to do, instead of renaming the folder – lunadir Jan 17 '22 at 07:44
  • I had to ensure my `core.ignorecase` was configured to be `true` on Windows, and in between the two `git mv`s I had to manually rename the directory to the right casing – Cloud Feb 20 '22 at 01:12
  • This fails for me under Mac OS, with "fatal: source directory is empty" (which is not true). This happens even if I use totally different names. – Oscar Feb 25 '22 at 22:43
  • It helps if it's a clean *clone* of the repo. At least if you're doing a .net project, I think. I found that if I had `bin` and `obj` folders present, `git mv` it nested badly. I ended up with a `` folder that contained an `` folder with `bin` and `obj` and a `` folder that had what should have been in the first `` folder. Fresh clone (or possibly a better clean than Visual Studio can do) fixed that. – GregHNZ Mar 17 '22 at 20:47
  • @orezvani Sadly no. `git log foo/folder` will show log of the folder, `git mv foo/folder folderNew; git commit ; git log folderNew` will only show the commit that renamed the folder. I'm still looking for a solution, maybe rewriting logs, like in this answer: https://stackoverflow.com/questions/2314652/ – David Balažic Jun 15 '23 at 09:26
188

If you receive this error: fatal: renaming ‘foldername’ failed: Invalid argument

Try this:

*nixOS

git mv foldername tempname && git mv tempname folderName

WinOS

git config core.ignorecase false; git mv foldername tempname; git mv tempname folderName

SliverNinja - MSFT
  • 31,051
  • 11
  • 110
  • 173
Jacques Betancourt
  • 2,652
  • 3
  • 18
  • 19
  • 14
    This is exactly what I needed to do a case-change in a directory. – cjserio Apr 02 '15 at 12:32
  • The token '&&' is not a valid statement separator in this version. git version 2.11.0.windows. – Tim Hardy Jun 26 '17 at 21:09
  • @Tim Hardy it can also be run as two separate commands, `git mv foldername tempname` and `git mv tempname folderName`, which should work on Windows. – Larkeith Oct 28 '18 at 02:57
  • 1
    this does not work ! the end result will put new 'folderName' folder inside the 'tempname' folder – bet Mar 29 '19 at 03:22
88

1. Change a folder's name from oldfolder to newfolder

git mv oldfolder newfolder

2. If newfolder is already in your repository & you'd like to override it and use:- force

git mv -f oldfolder newfolder

Don't forget to add the changes to index & commit them after renaming with git mv.

3. Renaming foldername to folderName on case insensitive file systems

Simple renaming with a normal mv command(not git mv) won’t get recognized as a filechange from git. If you try it with the ‘git mv’ command like in the following line

git mv foldername folderName

If you’re using a case insensitive filesystem, e.g. you’re on a Mac and you didn’t configure it to be case sensitive, you’ll experience an error message like this one:

fatal: renaming ‘foldername’ failed: Invalid argument

And here is what you can do in order to make it work:-

git mv foldername tempname && git mv tempname folderName

This splits up the renaming process by renaming the folder at first to a completely different foldername. After renaming it to the different foldername the folder can finally be renamed to the new folderName. After those ‘git mv’s, again, do not forget to add and commit the changes. Though this is probably not a beautiful technique, it works perfectly fine. The filesystem will still not recognize a change of the letter cases, but git does due to renaming it to a new foldername, and that’s all we wanted :)

akshay_rahar
  • 1,661
  • 2
  • 18
  • 20
23

lots of correct answers, but as I landed here to copy & paste a folder rename with history, I found that this

git mv <old name> <new name>

will move the old folder (itself) to nest within the new folder

while

git mv <old name>/ <new name>

(note the '/') will move the nested content from the old folder to the new folder

both commands didn't copy along the history of nested files. I eventually renamed each nested folder individually

git mv <old name>/<nest-folder> <new name>/<nest-folder>
Yinon
  • 697
  • 7
  • 13
15

You can rename the directory using the file system. Then you can do git rm <old directory> and git add <new directory> (Help page). Then you can commit and push.

Git will detect that the contents are the same and that it's just a rename operation, and it'll appear as a rename entry in the history. You can check that this is the case before the commit using git status

Chan
  • 2,601
  • 6
  • 28
  • 45
Oleksi
  • 12,947
  • 4
  • 56
  • 80
  • 28
    hey, but this way, I'll loose whole commit history. – ViliusK Feb 09 '14 at 20:59
  • 3
    You can retain it if you use the -follow flag. – Oleksi Feb 10 '14 at 04:01
  • 2
    Two commands instead of one, and having to add a flag? Is this better than git mv in any way? – antgel Jul 03 '14 at 04:56
  • 7
    @topper No, git mv is just an alias for rm+add. Using git mv is a better solution. – Oleksi Jul 03 '14 at 13:43
  • 7
    @topper Note that you'll still have to use --follow to view the history regardless of which method you use to move the file. – Oleksi Jul 03 '14 at 13:43
  • Renaming in the file system before you do the appropriate git actions does have the benefit of allowing you to see the effects of a change within a make file or project build, so you can correct anything that breaks during a rename within the same commit. – ajgryc Jun 11 '19 at 01:15
  • Also, in case you have used the file system without thinking, and then have to fix it so git understands, this is the way to go. – Igor Rivin Feb 07 '20 at 14:09
  • `git mv` did not work for me, but this worked. – Nilesh Thakkar Aug 04 '23 at 21:45
5

Here's an example for renaming directory.

git mv src/dir1/ src/dir2/

If you get an error stating Permission Denied, you can try

git mv src/dir1 src/temp/
git mv src/temp src/dir2
Kritish Bhattarai
  • 1,501
  • 15
  • 20
4

From Web Application I think you can't, but you can rename all the folders in Git Client, it will move your files in the new renamed folders, than commit and push to remote repository.

I had a very similar issue: I had to rename different folders from uppercase to lowercase (like Abc -> abc), I've renamed all the folders with a dummy name (like 'abc___') and than committed to remote repository, after that I renamed all the folders to the original name with the lowercase (like abc) and it took them!

SyncroIT
  • 1,510
  • 1
  • 14
  • 26
2

For case sensitive renaming, git mv somefolder someFolder has worked for me before but didn't today for some reason. So as a workaround I created a new folder temp, moved all the contents of somefolder into temp, deleted somefolder, committed the temp, then created someFolder, moved all the contents of temp into someFolder, deleted temp, committed and pushed someFolder and it worked! Shows up as someFolder in git.

Ryan Walker
  • 715
  • 2
  • 10
  • 23
  • Imo we don't really need a temp folder, required steps: 1. Rename somefolder to any other name (for example somefolder1) 2. Commit and push changes 3. Rename somefolder1 to someFolder 4. Commit and push changes – tezyakov Jan 19 '22 at 12:19
2

I tried with the following command and it didn't work. I was receiving a fatal: renaming '...' failed: Invalid argument error.

git mv oldName NewName

Then solved with the following method:

  1. First I duplicate the folder I wanted to rename
  2. Then I ran the following command to remove the folder
git rm oldName -r
  1. Renamed the duplicated folder to NewName
Shamshad Zaheer
  • 221
  • 3
  • 11
1

Just a heads up, the accepted answer won't work unless you give complete folder paths. Otherwise, you'd get fatal: bad source error.

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32271289) – The Dreams Wind Jul 21 '22 at 11:49
1

Simple Trick

You can just rename the directory with any temp name and then rename again and it will work

Kanishka Sahu
  • 119
  • 2
  • 3
0

renaming in git is difficult because the index will have to change and the tree object will be created after commit. I had the problem of renaming templates to Templates... I solved the problem by

  • copying Templates to templates in bash [cp -r Templates templates ] (git mv Templates templates will not work)
  • removing Templates in bash [rm -r Templates ](check that the copying was successful first)
  • Removing the Templates file from the index[use "git ls-files -s" to see the index, "git rm " you can use wildcards such as git rm Templates/*, continue checking the index]
  • Adding the renamed paths to the index ("git add -v ." and check the result with "git ls-files -s"
  • Commit ["git commit -m "renaming ... "
  • If you have remotes git push <to wherever origin,
Andre
  • 1
-26

Simply rename the folder. git is a "content-tracker", so the SHA1 hashes are the same and git knows, that you rename it. The only thing that changes is the tree-object.

$ rm <directory> // remove the directory
$ git add . // add changes to the git
$ git commit // commit removed directory
Community
  • 1
  • 1
bitwave
  • 347
  • 1
  • 3
  • 10