在 Git 存储库中,如何正确重命名目录?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11183788/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In a Git repository, how to properly rename a directory?
提问by qazwsx
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?
我认为它应该工作复制到被重命名为与所需名称的新目录的目录,并删除旧的目录,并且git add
, git commit
和push
一切。但这是最好的方法吗?
回答by CB Bailey
Basic rename (or move):
基本重命名(或移动):
git mv <old name> <new name>
Case sensitive rename—eg. from casesensitive
to CaseSensitive
—you must use a two step:
区分大小写的重命名——例如。从casesensitive
到CaseSensitive
- 你必须使用两个步骤:
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.
...接着提交和推送将是重命名 git 存储库中目录的最简单方法。
回答by Jacques Betancourt
If you receive this error: fatal: renaming ‘foldername' failed: Invalid argument
如果您收到此错误:致命:重命名“文件夹名”失败:参数无效
Try this:
尝试这个:
git mv foldername tempname && git mv tempname folderName
git mv foldername tempname && git mv tempname folderName
回答by akshay_rahar
1. Change a folder's name from oldfolder to newfolder
1. 将文件夹的名称从 oldfolder 更改为 newfolder
git mv oldfolder newfolder
2. If newfolder is already in your repository & you'd like to override it and use:-force
2.如果新文件夹已经在您的存储库中并且您想覆盖它并使用:- force
git mv -f oldfolder newfolder
Don't forget to add the changes to index & commit them after renaming with git mv.
不要忘记在使用 git mv 重命名后将更改添加到索引并提交它们。
3. Renaming foldername to folderName on case insensitive file systems
3. 在不区分大小写的文件系统上将文件夹名重命名为文件夹名
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
使用普通 mv 命令(不是 git mv)进行的简单重命名不会被识别为来自 git 的文件更改。如果您使用“git mv”命令进行尝试,如下行所示
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:
如果您使用的是不区分大小写的文件系统,例如您在 Mac 上并且没有将其配置为区分大小写,您将遇到如下错误消息:
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 :)
这通过首先将文件夹重命名为完全不同的文件夹名称来拆分重命名过程。将其重命名为不同的文件夹名称后,该文件夹最终可以重命名为新的文件夹名称。在那些 'git mv's 之后,再次不要忘记添加和提交更改。虽然这可能不是一个漂亮的技术,但它工作得很好。文件系统仍然无法识别字母大小写的更改,但是由于将其重命名为新的文件夹名称,git 可以识别,这就是我们想要的:)
回答by Oleksi
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 rm <old directory>
和git add <new directory>
(帮助页面)。然后你可以提交和推送。
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
Git会检测到内容是一样的,只是重命名操作,会在历史记录中显示为重命名条目。您可以在使用提交之前检查情况是否如此git status
回答by Syncro
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.
从 Web 应用程序我认为你不能,但你可以重命名 Git 客户端中的所有文件夹,它会将你的文件移动到新重命名的文件夹中,而不是提交并推送到远程存储库。
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!
我有一个非常相似的问题:我必须将不同的文件夹从大写重命名为小写(如 Abc -> abc),我用虚拟名称(如“abc___”)重命名了所有文件夹,然后提交到远程存储库,之后我将所有文件夹重命名为带有小写字母的原始名称(如 abc),并使用了它们!
回答by Nilesh
I solved it in two steps. To rename folder using mv command you need rights to do so, if you don't have right you can follow these steps. Suppose you want to rename casesensitive to Casesensitive.
我分两步解决了。要使用 mv 命令重命名文件夹,您需要这样做的权限,如果您没有权限,您可以按照以下步骤操作。假设您想将 casesensitive 重命名为 Casesensitive。
Step 1: Rename the folder (casesensitive) to something else from explorer. eg Rename casesensitive to folder1 commit this change.
步骤 1:将文件夹(区分大小写)重命名为资源管理器中的其他名称。例如,将区分大小写的重命名为folder1 提交此更改。
Step 2: Rename this newly named folder(folder1) to the expected case sensitive name (Casesensitive ) eg. Rename folder1 to Casesensitive. Commit this change.
第 2 步:将这个新命名的文件夹(folder1)重命名为预期的区分大小写的名称(Casesensitive),例如。将 folder1 重命名为区分大小写。提交此更改。
回答by Ryan Walker
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.
对于区分大小写的重命名,git mv somefolder someFolder
以前对我有用,但由于某种原因今天没有。因此,作为一种解决方法,我创建了一个新文件夹temp
,将所有内容移动somefolder
到temp
,删除somefolder
,提交temp
,然后创建someFolder
,移动temp
到someFolder
,删除temp
,提交和推送的所有内容,someFolder
它起作用了!someFolder
在 git 中显示。
回答by bitwave
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.
只需重命名文件夹。git 是一个“内容跟踪器”,所以 SHA1 哈希是相同的,git 知道,你重命名了它。唯一改变的是树对象。
rm <directory>
git add .
git commit