How do I use octal characters in a git checkout?

2019-05-24 13:32发布

问题:

I have a git repo of my music files, and I have recently detected that some are deleted. I suppose I accidentally deleted them, however now I have only too-briefly reveled in the fact that I had the foresight to use a git repo, so my current git status verifies they have been deleted. Here is the listing of a few:

# deleted:  Steve_Erquiaga-Cafe_Paradiso/03 - Arioso__J.S._Bach_.flac
# deleted: "Steve_Erquiaga-Cafe_Paradiso/04 - S\303\203\302\251r\303\203\302\251nade__Rachmaninov__from_Moreaux_de_fantasie.flac"
# deleted: "Steve_Erquiaga-Cafe_Paradiso/05 - After_a_Dream__Faure___Apr\303\203\302\250s_Une_R\303\203\302\252ve_.flac"
# deleted:  Steve_Erquiaga-Cafe_Paradiso/06 - Prelude_in_C_Minor__J.S._Bach__from_The_Well-Tempered_Clavier.flac
# deleted:  Steve_Erquiaga-Cafe_Paradiso/07 - If_Dreams_Could_Dance__Erquiaga_.flac

I have already restored one song that did not contain special characters in its name by using:

$git checkout "copy/pasted-in_name_from the output above"

However the special characters in some of the names are blocking my simplistic approach to restore them:

$git checkout 'Steve_Erquiaga-Cafe_Paradiso/05 - After_a_Dream__Faure___Apr\303\203\302\250s_Une_R\303\203\302\252ve_.flac'

Error:

pathspec 'Steve_Erquiaga-Cafe_Paradiso/05 - After_a_Dream_Faure__Apr\303\203\302\250s_Une_R\303\203\302\252ve_.flac' did not match any file(s) known to git.

I suppose it is not a git issue per se, rather a shell special-character escaping or translating issue. I tried putting the name in double quotes, as it is displayed, but I got the same message. I also tried it with outer double-quotes and preceding each 'inner double quote' with a backslash, garnering the same type of error message.

How can I construct a git checkout command that will work using these file names?

回答1:

You can try using printf, which understands 3-character octal escapes, e.g.:

git checkout -- "$(printf "Steve_Erquiaga-Cafe_Paradiso/05 - After_a_Dream__Faure___Apr\303\203\302\250s_Une_R\303\203\302\252ve_.flac")"

I haven't tested this, I'm afraid, but I think it should work.



回答2:

Try using double quotes instead of single quotes in your git checkout command. Escape sequences generally work inside double-quoted strings but not single quoted.

And here's an even easier idea, but it could erase local changes:

git checkout .