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

yyyllmsg  于 2023-05-21  发布在  Git
关注(0)|答案(1)|浏览(141)

我使用git bash在windows上进行git相关的活动。
我创建了一个分支,做了一些修改并提交。当我试着推的时候,它给了我:
错误"refpath does not exist"
然后我 checkout 到其他分支,并重试 checkout 到我的分支,但它说
错误: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

这背后的原因是什么,以及可能的解决办法是什么?

0ejtzxu1

0ejtzxu11#

不需要的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

相关问题