Git 提取无效的 Windows 文件名
Git pull invalid Windows file names
我正在使用 git 进行版本控制来处理一个共享项目。我在 windows 而我的伙伴在 Unix。
我的搭档用 <file1>.txt
命名了一些文件。当我尝试提取这些文件时,它们不被接受,因为 <
和 >
是 Windows 的无效字符。这很好,我不需要触摸文件。但是,它们被添加到我的提交中作为删除。所以,如果我按下,我将删除这些我不想做的文件。
我无法使用 git reset --hard
,因为它为每个 "deleted" 文件找到了无效路径。
有没有办法从我的提交中排除这些文件?我试过将 <file1>
添加到我的 .git/info/exclude
,但没有用。
如果文件已被跟踪,则忽略无济于事。
使用 Sparse checkout 跳过这些文件。
您需要让您的合作伙伴将名称更改为在 Windows 上也有效的名称。在他们重命名之后,我要做的是:
- 备份您仅在本地进行的任何更改(未提交和已提交但未推送)。
- 运行
git reset --hard <commit>
其中 <commit>
是添加文件之前的任何提交。
- 运行
git pull
以获取最新版本(文件已重命名)。
- 从 1.
恢复您备份的更改
这应该得到较新的修订版,其中文件未在此命名,以 Windows 非法方式,并且它们不会从 [=52= 下删除(或创建) ] 由 OS :)
P.S。我知道这是一个老问题,但我最近一直遇到这个问题,所以希望我找到的解决方案也能帮助其他人。
编辑:
为避免再次发生这种情况,您的合作伙伴可以添加一个预提交挂钩,以阻止他们提交名称在 Windows 上不允许的文件。在commit.sample之前经常有一个sample/example。我过去稍微改变了一下,最后得到的是:
# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
echo $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C)
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ !#-)+-.0-9;=@-[]-{}~][=10=]' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.
This can cause problems if you want to work with people on other platforms.
To be portable it is advisable to rename the file.
If you know what you are doing you can disable this check using:
git config hooks.allownonascii true
EOF
exit 1
fi
'[ !#-)+-.0-9;=@-[]-{}~][=14=]'
位是重要的部分,我做了一些改动。它定义了所有允许的字符范围,示例一仅不允许 "non-ascii" 字符(这是顶部的注释所说的),但在 [=48 上的文件名中也有不允许的 ascii 字符=](例如 ?
和 :
)。
所有允许的字符都被删除,如果还有任何内容 (wc -c != 0
) 则会出错。它可能有点难以阅读,因为您看不到任何不允许的字符。如果您有一个字符范围列表可以在阅读或编辑时查看,这会有所帮助。
我正在使用 git 进行版本控制来处理一个共享项目。我在 windows 而我的伙伴在 Unix。
我的搭档用 <file1>.txt
命名了一些文件。当我尝试提取这些文件时,它们不被接受,因为 <
和 >
是 Windows 的无效字符。这很好,我不需要触摸文件。但是,它们被添加到我的提交中作为删除。所以,如果我按下,我将删除这些我不想做的文件。
我无法使用
git reset --hard
,因为它为每个 "deleted" 文件找到了无效路径。有没有办法从我的提交中排除这些文件?我试过将
<file1>
添加到我的.git/info/exclude
,但没有用。
如果文件已被跟踪,则忽略无济于事。
使用 Sparse checkout 跳过这些文件。
您需要让您的合作伙伴将名称更改为在 Windows 上也有效的名称。在他们重命名之后,我要做的是:
- 备份您仅在本地进行的任何更改(未提交和已提交但未推送)。
- 运行
git reset --hard <commit>
其中<commit>
是添加文件之前的任何提交。 - 运行
git pull
以获取最新版本(文件已重命名)。 - 从 1. 恢复您备份的更改
这应该得到较新的修订版,其中文件未在此命名,以 Windows 非法方式,并且它们不会从 [=52= 下删除(或创建) ] 由 OS :)
P.S。我知道这是一个老问题,但我最近一直遇到这个问题,所以希望我找到的解决方案也能帮助其他人。
编辑:
为避免再次发生这种情况,您的合作伙伴可以添加一个预提交挂钩,以阻止他们提交名称在 Windows 上不允许的文件。在commit.sample之前经常有一个sample/example。我过去稍微改变了一下,最后得到的是:
# Cross platform projects tend to avoid non-ASCII filenames; prevent
# them from being added to the repository. We exploit the fact that the
# printable range starts at the space character and ends with tilde.
if [ "$allownonascii" != "true" ] &&
# Note that the use of brackets around a tr range is ok here, (it's
# even required, for portability to Solaris 10's /usr/bin/tr), since
# the square bracket bytes happen to fall in the designated range.
echo $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C)
test $(git diff --cached --name-only --diff-filter=A -z $against |
LC_ALL=C tr -d '[ !#-)+-.0-9;=@-[]-{}~][=10=]' | wc -c) != 0
then
cat <<\EOF
Error: Attempt to add a non-ASCII file name.
This can cause problems if you want to work with people on other platforms.
To be portable it is advisable to rename the file.
If you know what you are doing you can disable this check using:
git config hooks.allownonascii true
EOF
exit 1
fi
'[ !#-)+-.0-9;=@-[]-{}~][=14=]'
位是重要的部分,我做了一些改动。它定义了所有允许的字符范围,示例一仅不允许 "non-ascii" 字符(这是顶部的注释所说的),但在 [=48 上的文件名中也有不允许的 ascii 字符=](例如 ?
和 :
)。
所有允许的字符都被删除,如果还有任何内容 (wc -c != 0
) 则会出错。它可能有点难以阅读,因为您看不到任何不允许的字符。如果您有一个字符范围列表可以在阅读或编辑时查看,这会有所帮助。