英文:
Using Git Bash on Windows, how do I checkout/rename a branch that begins with a Unicode character?
问题
I use git bash for git related activities on windows.
我在Windows上使用Git Bash进行与Git相关的操作。
I created a branch, did some changes and committed. When I tried to push, it gives me:
我创建了一个分支,进行了一些更改并提交了。当我尝试推送时,它给我报错:
error "refpath does not exist".
错误信息是“refpath不存在”。
I then checkout to other branch and retried to checkout to my branch but it says
然后我切换到其他分支并尝试再次切换到我的分支,但它显示错误:
error: pathspec '<U+0085><U+0085><U+0085><U+0086>feature/my-branch' did not match any file(s) known to git.
错误信息是:路径规范'<U+0085><U+0085><U+0085><U+0086>feature/my-branch'与Git中已知的任何文件不匹配。
on running git branch, it lists my branch name as-
运行git branch命令后,它列出我的分支名称为-
<U+0085><U+0085><U+0085><U+0086>feature/my-branch
<U+0085><U+0085><U+0085><U+0086>feature/my-branch
I tried renaming this branch but that also didn't work.
我尝试重命名这个分支,但也没有成功。
git branch -m '<U+0085><U+0085><U+0085><U+0086>feature/my-branch' feature/new-branch
git branch -m '<U+0085><U+0085><U+0085><U+0086>feature/my-branch' feature/new-branch
what is the reason behind this and what could be the possible solution?
这背后的原因是什么,可能的解决方案是什么?
英文:
I use git bash for git related activities on windows.
I created a branch, did some changes and committed. When I tried to push, it gives me:
error "refpath does not exist"
.
I then checkout to other branch and retried to checkout to my branch but it says
error: pathspec 'feature/my-branch'
did not match any file(s) known to git.
on running git branch, it lists my branch name as-
<U+0085><U+0085><U+0085><U+0086>feature/my-branch
I tried renaming this branch but that also didn't work.
git branch -m '<U+0085><U+0085><U+0085><U+0086>feature/my-branch' feature/new-branch
what is the reason behind this and what could be the possible solution?
答案1
得分: 2
The unwanted unicode characters are converted to sequences such as <U+0085>
or <U+0086>
当它们在你的终端上被打印时.
faulty=$(git branch | grep -a feature/my-branch)
应该保存那个分支名称的“正确”值,所以这应该可以工作:
# '-a'选项用于跳过grep对二进制内容的自动检测
# 如果输入中有一个'\x00'字节,它可能会触发
#
# 对于特定的问题(涉及<U+0085>字符),它不应该触发,我只是
# 提及该选项以便更广泛地考虑
faulty=$(git branch | grep -a feature/my-branch)
# 为了避免在输出中出现前导空格和可能的'*':
faulty=$(git branch --format="%(refname:short)" | grep -a feature/my-branch)
git branch -m "$faulty" feature/my-branch
否则:
- bash 本身应该知道
使用 $'...'
语法来使带有转义序列的字符串被解释:
git branch -m $'\u0085\u0085\u0085\u0086feature/my-branch' feature/my-branch
printf
知道\uXXXX
序列
你可以尝试运行:
faulty=$(printf "\u0085\u0085\u0085\u0086feature/my-branch")
# 你可以检查 'echo "$faulty"' 是否给你与 'git branch' 相同的输出
git branch -m "$faulty" feature/my-branch
英文:
The unwanted unicode chracters are converted to sequences such as <U+0085>
or <U+0086>
when they are printed on your terminal.
faulty=$(git branch | grep -a feature/my-branch)
should hold the "correct" value for that branch name, so this should work:
# 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
Otherwise:
- bash itself should know
use the $'...'
syntax to have a string with the escape sequences interpreted:
git branch -m $'\u0085\u0085\u0085\u0086feature/my-branch' feature/my-branch
printf
knows about\uXXXX
sequences
you can try to run :
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论