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?
-
1Possible duplicate: How to tell Git that it's the same directory, just a different name– Christopher PeisertCommented Jun 25, 2012 at 5:35
-
6As 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 EppCommented Jun 25, 2012 at 6:02
14 Answers
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.
-
8
-
54@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– dtothefpCommented Nov 20, 2014 at 22:09 -
9But 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?– AhmedCommented Feb 23, 2016 at 19:58
-
7Thanks 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 asource is empty
error.– HosseinCommented May 18, 2020 at 4:32 -
5On 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.– arcainCommented Jun 25, 2021 at 16:45
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
-
16This is exactly what I needed to do a case-change in a directory.– cjserioCommented Apr 2, 2015 at 12:32
-
The token '&&' is not a valid statement separator in this version. git version 2.11.0.windows. Commented Jun 26, 2017 at 21:09
-
@Tim Hardy it can also be run as two separate commands,
git mv foldername tempname
andgit mv tempname folderName
, which should work on Windows.– LarkeithCommented Oct 28, 2018 at 2:57 -
1this does not work ! the end result will put new 'folderName' folder inside the 'tempname' folder– betCommented Mar 29, 2019 at 3:22
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 :)
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>
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
-
29
-
4
-
2Two commands instead of one, and having to add a flag? Is this better than git mv in any way?– antgelCommented Jul 3, 2014 at 4:56
-
8@topper No, git mv is just an alias for rm+add. Using git mv is a better solution.– OleksiCommented Jul 3, 2014 at 13:43
-
8@topper Note that you'll still have to use --follow to view the history regardless of which method you use to move the file.– OleksiCommented Jul 3, 2014 at 13:43
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
-
1
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!
Simple Trick
You can just rename the directory with any temp name and then rename again and it will work
-
1This is a great trick! saved a lot of time, thank you Commented Feb 23, 2023 at 17:24
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.
-
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– tezyakovCommented Jan 19, 2022 at 12:19
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:
- First I duplicate the folder I wanted to rename
- Then I ran the following command to remove the folder
git rm oldName -r
- Renamed the duplicated folder to
NewName
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 you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From Review Commented Jul 21, 2022 at 11:49
I had an issue using the git mv <old dir> <new dir>
command as it nests it.
The solution that worked for me was the simple one:
- rename the file using File Explorer
git add .
Then git status
shows the files were renamed
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,
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
-
5This does not work always. It surely did not work for me for sth. like 20% files... Commented Sep 30, 2013 at 15:22