Search code examples
bashgitunicodegit-bash

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.

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?


Solution

  • 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