Remove a file with a strange name from git

前端 未结 3 1814
感动是毒
感动是毒 2020-12-10 14:31

I have a file that managed to get itself named:

# file\'s name (both lines)
companies.yml
companies.yml

# entry in my git working tree
"test/fixtures/co         


        
3条回答
  •  旧巷少年郎
    2020-12-10 15:12

    Since you are in bash, you could use the printf command:

    git rm --cached `printf "test/fixtures/companies.yml\342\200\250companies.yml"`
    

    Octal escapes beginning with \0 may contain up to four digits.
    (POSIX specifies up to three)

    You can see a similar solution at "Git: how to specify file names containing octal notation on the command line".

    That will remove the file from the index while still keeping it on the working tree.
    You can then rename it if you want.


    Update with Git 2.18 (Q2 2018, 6 years later), using a simple completion should work.

    See commit 3dfe23b (16 Apr 2018) by SZEDER Gábor (szeder).

    completion: support completing non-ASCII pathnames

    Unless the user has 'core.quotePath=false' somewhere in the configuration, both 'git ls-files' and 'git diff-index' will by default quote any pathnames that contain bytes with values higher than 0x80, and escape those bytes as '\nnn' octal values.
    This prevents completing paths when the current path component to be completed contains any non-ASCII, most notably UTF-8, characters, because none of the listed quoted paths will match the current word on the command line.

    Set 'core.quotePath=false' for those 'git ls-files' and 'git diff-index' invocations, so they won't consider bytes higher than 0x80 as "unusual", and won't quote pathnames containing such characters.

    Note that pathnames containing backslash, double quote, or control characters will still be quoted; a later patch in this series will deal with those.

提交回复
热议问题