简体   繁体   中英

Git: How do I revert the staged deleting of a unicode named file?

在此处输入图片说明

As the image shows, I deleted a file whose name containing some UniCode characters and added it to index. How to cancel that action?

More explain:

As the git status shows, I staged the deleted file. But I haven't committed it yet.

By git checkout -- deleteFile , it claims:

fatal: pathspec '03.08 T&D \\347\\224...' did not match any files.

By `git rm --cached "03.08 T&D \\347\\224..." it claims:

fatal: pathspec '03.08 T&D \\347\\224...' did not match any files. too.

在此处输入图片说明

The issue here is that the name of the file contains Unicode sequences, representing non-ASCII characters in the file name:

$ printf '%b\n' '03.08 T&D \347\224\260\346\235\260 WeeklyReport.md'
03.08 T&D 田杰 WeeklyReport.md

When git status prints the file name, it's not sure if it is OK to produce Chinese script on your screen, so it writes out the octal values of the UTF-8 sequence that would have produced 田 and 杰, rather than printing them. [ Edit: if you set core.quotePath to false , that tells Git that it is OK to print such characters without replacing them with escape sequences. See below.]

The above printf command shows one way to express the file name in a way that allows you to cut and paste it. You could then use:

git checkout -- '03.08 T&D 田杰 WeeklyReport.md'

to get it back.

What about git stash ?

The method you used—running git stash —amounts to making a commit from the change that you had staged, then running git reset --hard . The commit that git stash made is not on any branch. Running git stash list will show the commit that git stash made. You can do whatever you want with this later.

Technically, git stash actually made two commits that are on no branch, but in this case only one of them did anything useful: it saved the deletion of the file, in case you really did mean to delete it. That's not very hard to reproduce (you can just delete the file again), so you can simply drop the stash:

git stash drop

as long as there is nothing else of value in it.

Using core.quotePath

Again, I don't have your repository here, so I had to just make my own guesses about files. Here's what I did:

$ mkdir tmp/tpath; cd tmp/tpath; git init
Initialized empty Git repository in .../tmp/tpath/.git/
$ echo text > '03.08 T&D 田杰 WeeklyReport.md'
$ git add .
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   "03.08 T&D \347\224\260\346\235\260 WeeklyReport.md"

$ git config core.quotePath false
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)

        new file:   03.08 T&D 田杰 WeeklyReport.md

$ git commit -m initial
[master (root-commit) 5658907] initial
 1 file changed, 1 insertion(+)
 create mode 100644 03.08 T&D 田杰 WeeklyReport.md

If you're on Windows or MacOS, be aware that some UTF-8 file names can run into various translation issues, in ways similar to cases where a Linux user can create two different files named readme and README and commit them. If you never move from your Windows or MacOS system to Linux and vice versa, you won't run into problems, but if you do, you may. (This is more commonly a problem with accented characters in file names like agréable or schön . Unicode has multiple ways to spell these names, but MacOS in particular insists on spelling them its way.)

git checkout HEAD -- <filepath>

那应该工作

Don't know the cause. But I worked around it by

git stash

Thanks for answering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM