在 Windows 上使用 Git Bash,如何签出/重命名以 Unicode 字符开头的分支?

问题描述 投票:0回答:1

我在 Windows 上使用 git bash 进行与 git 相关的活动。

我创建了一个分支,做了一些更改并提交了。当我试图推动时,它给了我:

错误

"refpath does not exist"
.

然后我结帐到其他分行并重新尝试结帐到我的分行,但它说

error: pathspec

'feature/my-branch'
与 git 已知的任何文件都不匹配。

在运行 git branch 时,它将我的分支名称列为 -

<U+0085><U+0085><U+0085><U+0086>feature/my-branch

我尝试重命名这个分支,但也没有用。

git branch -m  '<U+0085><U+0085><U+0085><U+0086>feature/my-branch' feature/new-branch

这背后的原因是什么,可能的解决方案是什么?

bash git unicode git-bash
1个回答
1
投票

不需要的 unicode 字符在您的终端上打印时会转换为

<U+0085>
<U+0086>
等序列

faulty=$(git branch | grep -a feature/my-branch)
应该保持该分支名称的“正确”值,所以这应该有效:

# The '-a' option to grep is there to skip grep's auto detection of binary content
# it could kick in if there was a '\x00' byte in the input
#
# Your specific issue (with <U+0085> characters) shouldn't trigger it, I'm just
# mentioning that option for a more general scope
faulty=$(git branch | grep -a feature/my-branch)

# to avoid shenanigans with leading spaces and a possible '*' in the output:
faulty=$(git branch --format="%(refname:short)" | grep -a feature/my-branch)

git branch -m "$faulty" feature/my-branch

否则:

printf
知道如何解释
\uXXXX
序列。

你可以尝试运行:

faulty=$(printf "\u0085\u0085\u0085\u0086feature/my-branch")
# you can check if 'echo "$faulty"' gives you the same output as 'git branch'

git branch -m "$faulty" feature/my-branch

和 bash 本身应该知道,当使用

$'...'
语法让它解释转义序列时:

git branch -m $'\u0085\u0085\u0085\u0086feature/my-branch' feature/my-branch
© www.soinside.com 2019 - 2024. All rights reserved.