Using Git Bash on Windows, how do I checkout/rename a branch that begins with a Unicode character?

huangapple go评论72阅读模式
英文:

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 &quot;refpath does not exist&quot;.

I then checkout to other branch and retried to checkout to my branch but it says

error: pathspec &#39;feature/my-branch&#39; did not match any file(s) known to git.

on running git branch, it lists my branch name as-

&lt;U+0085&gt;&lt;U+0085&gt;&lt;U+0085&gt;&lt;U+0086&gt;feature/my-branch

I tried renaming this branch but that also didn't work.

git branch -m  &#39;&lt;U+0085&gt;&lt;U+0085&gt;&lt;U+0085&gt;&lt;U+0086&gt;feature/my-branch&#39; 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 &lt;U+0085&gt; or &lt;U+0086&gt; 当它们在你的终端上被打印时.

faulty=$(git branch | grep -a feature/my-branch) 应该保存那个分支名称的“正确”值,所以这应该可以工作:

# &#39;-a&#39;选项用于跳过grep对二进制内容的自动检测
# 如果输入中有一个&#39;\x00&#39;字节,它可能会触发
#
# 对于特定的问题(涉及&lt;U+0085&gt;字符),它不应该触发,我只是
# 提及该选项以便更广泛地考虑
faulty=$(git branch | grep -a feature/my-branch)

# 为了避免在输出中出现前导空格和可能的&#39;*&#39;:
faulty=$(git branch --format=&quot;%(refname:short)&quot; | grep -a feature/my-branch)

git branch -m &quot;$faulty&quot; feature/my-branch

否则:

  • bash 本身应该知道

使用 $&#39;...&#39; 语法来使带有转义序列的字符串被解释:

git branch -m $&#39;\u0085\u0085\u0085\u0086feature/my-branch&#39; feature/my-branch
  • printf 知道 \uXXXX 序列

你可以尝试运行:

faulty=$(printf &quot;\u0085\u0085\u0085\u0086feature/my-branch&quot;)
# 你可以检查 &#39;echo &quot;$faulty&quot;&#39; 是否给你与 &#39;git branch&#39; 相同的输出

git branch -m &quot;$faulty&quot; feature/my-branch
英文:

The unwanted unicode chracters are converted to sequences such as &lt;U+0085&gt; or &lt;U+0086&gt; 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 &#39;-a&#39; option to grep is there to skip grep&#39;s auto detection of binary content
# it could kick in if there was a &#39;\x00&#39; byte in the input
#
# Your specific issue (with &lt;U+0085&gt; characters) shouldn&#39;t trigger it, I&#39;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 &#39;*&#39; in the output:
faulty=$(git branch --format=&quot;%(refname:short)&quot; | grep -a feature/my-branch)

git branch -m &quot;$faulty&quot; feature/my-branch

Otherwise:

  • bash itself should know

use the $&#39;...&#39; syntax to have a string with the escape sequences interpreted:

git branch -m $&#39;\u0085\u0085\u0085\u0086feature/my-branch&#39; feature/my-branch
  • printf knows about \uXXXX sequences

you can try to run :

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

git branch -m &quot;$faulty&quot; feature/my-branch

huangapple
  • 本文由 发表于 2023年5月17日 17:58:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270834.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定