Git-windows 区分大小写的文件名未正确处理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2528589/
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
Git-windows case sensitive file names not handled properly
提问by user298800
We have the git bare repository in unix that has files with same name that differs only in cases.
我们在 unix 中有 git 裸存储库,它具有相同名称的文件,仅在情况下有所不同。
Example:
例子:
GRANT.sql
grant.sql
When we clone the bare repository from unix in to a windows box, git status detects the file as modified. The working tree is loaded only with grant.sql, but git status compares grant.sql and GRANT.sql and shows the file as modified in the working tree.
当我们将裸存储库从 unix 克隆到 Windows 框中时,git status 会检测到该文件已修改。工作树仅使用 grant.sql 加载,但 git status 比较 grant.sql 和 GRANT.sql 并显示在工作树中修改的文件。
I tried using the core.ignorecase false but the result is the same.
我尝试使用 core.ignorecase false 但结果是一样的。
Is there any way to fix this issue?
有没有办法解决这个问题?
回答by J?rg W Mittag
Windows is case-insensitive (more precisely, case-preserving). There is simply no possible way for two files to exist whose names only differ in case: two filenames which differ only in case are the same filename. Period.
Windows 不区分大小写(更准确地说,保留大小写)。根本不可能存在名称仅大小写不同的两个文件:仅在大小写不同的两个文件名是相同的文件名。时期。
So, Git is walking the repository, checking out one file after the other, until it hits the first one of the two problem files. Git checks it out, then goes further about its business until it hits the second file. Again, Git checks it out. Since from Windows' point of view the filename is the same as the first one, the first file simply gets overwritten with the second one. Which now makes Git think that the first file was changed to have the same content as the second one.
因此,Git 正在遍历存储库,一个接一个地检出文件,直到遇到两个问题文件中的第一个。Git 检查它,然后进一步处理它的业务,直到它遇到第二个文件。再次,Git 检查它。由于从 Windows 的角度来看,文件名与第一个文件名相同,因此第一个文件只会被第二个文件覆盖。这让 Git 认为第一个文件已更改为与第二个文件具有相同的内容。
Note that this has nothing to do with Git: exactly the same would happen if you had a tarball, a zipfile or a Subversion repository.
请注意,这与 Git 无关:如果您有 tarball、zipfile 或 Subversion 存储库,情况将完全相同。
If you want to do development on multiple different platforms, you have to respect the restrictions of those platforms and you have to confine yourself to the lowest common denominator of all the platforms you support. Windows supports ADS, Linux doesn't. OSX supports resource forks, Windows doesn't. BSD supports case-sensitivity, Windows doesn't. So, you can't use any of those. That's just the way it is.
如果你想在多个不同的平台上进行开发,你必须尊重这些平台的限制,你必须将自己限制在你支持的所有平台的最小公分母上。Windows 支持 ADS,Linux 不支持。OSX 支持资源分叉,Windows 不支持。BSD 支持区分大小写,Windows 不支持。所以,你不能使用其中任何一个。就是那样子。
core.ignorecase
isn't going to help you here, because that handles exactly the oppositeproblem.
core.ignorecase
不会在这里帮助你,因为它处理完全相反的问题。
回答by Greg Hewgill
I just encountered a similar problem. In my case, the two files with similar names differing only in case were in a subdirectory that wasn't relevant on the Windows clone. Git 1.7 has a sparse checkoutfeature that lets you exclude certain files from a working copy. To exclude this directory:
我刚刚遇到了类似的问题。就我而言,名称相似的两个文件仅在大小写不同的情况下位于与 Windows 克隆无关的子目录中。Git 1.7 具有稀疏检出功能,可让您从工作副本中排除某些文件。要排除此目录:
git config core.sparsecheckout true
echo '*' >.git/info/sparse-checkout
echo '!unwanted_dir/' >>.git/info/sparse-checkout
git read-tree --reset -u HEAD
After this, the unwanted_dir/
subdirectory was completely gone from my working copy and Git continues to work with the rest of the files as normal.
在此之后,unwanted_dir/
子目录完全从我的工作副本中消失了,Git 继续照常处理其余文件。
If your GRANT.sql
and grant.sql
are not relevant on the Windows clone, then you can add their names to .git/info/sparse-checkout
to exclude those files specifically.
如果您的GRANT.sql
和grant.sql
与 Windows 克隆不相关,那么您可以添加它们的名称以.git/info/sparse-checkout
专门排除这些文件。
回答by Marcelo Cantos
I'm not sure this is even possible. Git's ignorecase handles discrepancies in the case of the one file. It won't work around Window's inability to have two filenames in the one directory that differ only by case.
我不确定这是否可能。Git 的 ignorecase 处理单个文件的差异。它不会解决 Window 无法在一个目录中拥有两个仅因大小写而异的文件名的问题。
FWIW, having two identical filenames but for their case is a really bad idea, even on Unix.
FWIW,即使在 Unix 上,具有两个相同的文件名但对于它们的情况来说也是一个非常糟糕的主意。
回答by djjeck
If you want to keep your repository friendly to non-case sensitive file systems, you can add a commit hook that prevents you to check in clashing files.
如果你想让你的仓库对不区分大小写的文件系统友好,你可以添加一个提交钩子来防止你签入冲突文件。
#!/bin/bash
# Save current state
git stash -u -q --keep-index || exit 1
# Get the list of clashing files in the whole repository
CLASHING=`find "$(git rev-parse --show-toplevel)" | sort | uniq -d -i`
# Restore previous state
git stash pop -q
if [[ $CLASHING ]]; then
echo "Found clashing files on case-insensitive file systems"
echo "$CLASHING"
exit 1
fi
exit 0
This script requires git version >= 1.7.7, because it uses stash -u, to avoid failing on untracked files.
此脚本需要 git 版本 >= 1.7.7,因为它使用 stash -u,以避免在未跟踪的文件上失败。
回答by Naylor
The simplest way to actually fix the issue is to rename one of the files so that they won't conflict on a case-insensitive file system like Windows or OS X.
实际解决问题的最简单方法是重命名其中一个文件,这样它们就不会在不区分大小写的文件系统(如 Windows 或 OS X)上发生冲突。
Following a commit from the Linux/Unix system where you can most easily address the problem everything will be fine on Windows after a pull. To prevent this problem from occurring you would need to add a commit hook similar to what djjeck suggested.
在 Linux/Unix 系统提交之后,您可以最轻松地解决问题,在 Windows 上拉取后一切都会好起来的。为了防止这个问题发生,你需要添加一个类似于 djjeck 建议的提交钩子。
The symptoms on Windows for this are very confusing and include:
Windows 上的症状非常令人困惑,包括:
- Files that always show as changed even if you revert them which makes changing branches or rebasing very difficult.
- Two copies of the file with the names differing only in case both showing changes in git gui
- 即使您还原它们,也始终显示为已更改的文件,这使得更改分支或变基变得非常困难。
- 名称不同的文件的两个副本,以防两者都显示 git gui 中的更改
Since both files can not coexist on a case insensitive platform you have to change one of the file names to avoid the trouble.
由于这两个文件不能在不区分大小写的平台上共存,因此您必须更改其中一个文件名以避免出现问题。
回答by gordy
Cygwin handles case sensitivity and funny characters in filenames much better than MSys.
Cygwin 处理文件名中的大小写敏感和有趣的字符比 MSys 好得多。
Change this registry key to enable case sensitivity in Windows:
更改此注册表项以在 Windows 中启用区分大小写:
HKLM\System\CurrentControlSet\Control\Session Manager\Kernel\ObCaseInsensitive=0
HKLM\System\CurrentControlSet\Control\Session Manager\Kernel\ObCaseInsensitive=0
See herefor some caveats in how case sensitivity is supported in Cygwin.
有关Cygwin 如何支持区分大小写的一些注意事项,请参见此处。