git mv 并仅更改目录的大小写
虽然我发现类似的问题我没有找到问题的答案
当我尝试通过 git mv FOO foo 将目录从 FOO 重命名为 foo 时,我得到了
fatal: renaming 'FOO' failed: Invalid argument
确定。所以我尝试 git mv FOO foo2 && git mv foo2 foo
但是当我尝试通过 git commit .
提交时,我得到
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# foo
nothing added to commit but untracked files present (use "git add" to track)
当我通过 git add foo
添加目录时没有任何变化,并且 git commit .
再次给我同样的消息。
我做错了什么?我以为我使用的是区分大小写的系统 (OSX),为什么我不能简单地重命名目录?
While I found similar question I didn't find an answer to my problem
When I try to rename the directory from FOO to foo via git mv FOO foo
I get
fatal: renaming 'FOO' failed: Invalid argument
OK. So I try git mv FOO foo2 && git mv foo2 foo
But when I try to commit via git commit .
I get
# On branch master
# Untracked files:
# (use "git add <file>..." to include in what will be committed)
#
# foo
nothing added to commit but untracked files present (use "git add" to track)
When I add the directory via git add foo
nothing changes and git commit .
gives me the same message again.
What am I doing wrong? I thought I'm using a case-sensitive system (OSX) why can't I simply rename the directory?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
您处于不区分大小写的环境中。此外,不带
-A
进行添加将不会处理 Git 理解的mv
的删除部分。 警告!确保执行此操作时周围没有其他更改或未跟踪的文件,否则它们将作为此更改的一部分提交!git stash -u
首先,执行此操作,然后git之后存储弹出
。继续:要解决此问题,请执行以下操作:这是更改工作目录、提交然后折叠 2 个提交的冗长方法。您可以只移动索引中的文件,但对于刚接触 git 的人来说,它可能不够明确地说明发生了什么。较短的版本是
正如评论之一所建议的,您还可以执行交互式变基(如果在 5 次提交前引入了错误的情况,则 git rebase -i HEAD~5 )来修复那里的情况并历史上根本没有出现过错误的案例。如果你这样做,你必须小心,因为从那时起提交哈希将会不同,其他人将不得不重新设置或重新合并他们的工作与分支的最近过去。
这与更正文件名有关: Is git not casesensitive?
You are in a case insensitive environment. Further, adding without the
-A
will not take care of the remove side of themv
as Git understands it. Warning! Ensure that no other changes or untracked files are around when you do this or they will get committed as part of this change!git stash -u
first, do this and thengit stash pop
after. Continuing: To get around this, do the following:That's the drawn out way of changing the working directory, committing and then collapsing the 2 commits. You can just move the file in the index, but to someone that is new to git, it may not be explicit enough as to what is happening. The shorter version is
As suggested in one of the comments, you can also do an interactive rebase (
git rebase -i HEAD~5
if the wrong case was introduced 5 commits ago) to fix the case there and not have the wrong case appear anywhere in the history at all. You have to be careful if you do this as the commit hashes from then on will be different and others will have to rebase or re-merge their work with that recent past of the branch.This is related to correcting the name of a file: Is git not case sensitive?
您希望将选项 core.ignorecase 设置为 false,这将使 Git 注意本机不支持它的文件系统上的大小写。要在您的存储库中启用:
然后您可以使用 git mv 重命名该文件,它将按预期工作。
You want to set the option
core.ignorecase
to false, which will make Git pay attention to case on file systems that don't natively support it. To enable in your repo:Then you can rename the file with
git mv
and it'll work as expected.我能够使用 git 1.7.7 通过使用临时文件名来解决此问题:
I was able to resolve this, using git 1.7.7 by using a temporary filename:
(
git mv
-free 变体。)我在 Mac OS X 10.9 上的 Git 中遇到了这个问题。我按如下方式解决了这个问题:
git rm -r --cached /path/to/directory
暂存目录以便在 Git 中删除,但实际上并没有删除任何物理文件 (
--cached< /代码>)。这也使得现在具有正确大小写的目录显示在未跟踪的文件中。
所以你可以这样做:
Git 然后会识别出你已经重命名了文件,当你执行 git status 时,你应该会看到许多
renamed:
行。检查它们并确保它们看起来正确,如果是这样,您可以正常提交更改。(
git mv
-free variant.)I ran into this problem in Git on Mac OS X 10.9. I solved it as follows:
git rm -r --cached /path/to/directory
That stages the directory for deletion in Git but does not actually remove any physical files (
--cached
). This also makes the directory, now with the proper case, show up in untracked files.So you can do this:
Git will then recognize that you have renamed the files, and when you do
git status
you should see a number ofrenamed:
lines. Inspect them and ensure they look correct, and if so, you can commit the changes normally.这是一个快速且无错误的解决方案:
警告!始终重命名重命名文件夹中的所有文件(使用
/*
)。不要重命名单个文件。这会导致一个错误,如本答案中所述。
如果您首先想先查看结果,请使用
-n
:在创建
mv
后:现在 Git 应该在其内部文件和文件系统中重命名该文件夹。
This is a quick and bug-safe solution:
Warning! Always rename all files in the renamed folder (use
/*
).Do not rename single files. This leads to a bug, described in this answer.
If you first want to see the outcome first, use
-n
:After you've made an
mv
:Now Git should have renamed the folder BOTH in its internal files and in file system.
使用 -f 选项强制执行:
Force it with -f option:
我有一个相关的问题。
一个名为“Pro”的文件夹(首先创建)和另一个名为“pro”的文件夹(错误创建)。在 Mac 中,这是同样的事情,但根据 git 有所不同。
git config 将文件重命名到正确的文件夹(谢谢),并且还在“pro”中创建了 Ghost 文件(不!!)。我无法将幽灵文件更改添加到轨道中,并且无法签出其他分支,除非随身携带这些文件,而且我也无法以某种方式重置它。
相反,我做了
为了使它更加安全,我在一个单独的修复分支中做了它,然后我合并回主分支
对于由创建的幽灵文件问题,任何大师都可以解释如何和为什么吗?
提前致谢。
I had one related issue.
One folder named 'Pro' (created first) and another 'pro' (created by mistake). In Mac, it is the same thing, but different according to git.
the git config rename the files to the right folder(thanks), and also created ghost files in 'pro' (No!!). I could not add ghost file changes to the track and I could not checkout other branches unless carry those those files with me, and i also could not reset it somehow.
Instead of that, i did
To make it extra safe, i did it in a separated fix branch, and then i merged back to main branch
For the ghost file issue created by , can any guru explain How and Why?
Thanks in advance.
这是一个简单的方法。
确保您的工作目录为空。
暂时禁用 git 忽略大小写
Here is a simple way of doing it.
Make sure your working directory is empty.
Temporarily disable git ignore case
除非您明确选择,否则您不会在 OS X 中使用区分大小写的文件系统。 HFS+可以区分大小写,但默认情况下不区分大小写。
You're not using a case-sensitive filesystem in OS X unless you explicitly choose such. HFS+ can be case-sensitive, but the default is case-insensitive.
这对我在 Windows 上非常有用。使用 powershell 进行以下操作:
mv .\Folder-With-Wrong-Casing .\temp
git add -A
git commit -m "用错误的大小写重命名文件夹to temp"
mv .\temp .\Folder-with-Correct-Casing
git add -A
git commit --amend -m "重命名正确的大小写”
git push
感谢上面 Adam 的回答。
This worked great for me on Windows. Used powershell with the following:
mv .\Folder-With-Wrong-Casing .\temp
git add -A
git commit -m "renamed folder with wrong casing to temp"
mv .\temp .\Folder-with-Correct-Casing
git add -A
git commit --amend -m "Renamed to proper casing"
git push
Thanks to Adam's answer above.
改进 Adam Dymitruk 的答案(愚蠢的是,SO 不允许我评论他的答案),使用“git mv”将自动准确地暂存移动的文件。不需要隐藏,并且可以避免有风险的“git add -A”:
Improving Adam Dymitruk's answer (silly that SO doesn't let me comment his answer), using "git mv" will automatically stage exactly the moved files. No stashing is needed and the risky "git add -A" can be avoided:
这是一个围绕此页面上所有 gitfoo 的非常简单的解决方案。
Here's a really simple solution around all the gitfoo on this page.