1

enter image description here

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.

enter image description here

Jeff Tian
  • 5,210
  • 3
  • 51
  • 71

3 Answers3

4

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.)

torek
  • 448,244
  • 59
  • 642
  • 775
  • Thanks for the detailed explanation! But the command `git checkout -- '03.08 T&D 田杰 WeeklyReport.md'` also claims the same error: `fatal: pathspec '03.08 T&D \347\224...' did not match any files`. – Jeff Tian Mar 18 '19 at 05:34
  • Thanks for the detailed explanation! But the command `git checkout -- '03.08 T&D 田杰 WeeklyReport.md'` also claims the same error: `fatal: pathspec '03.08 T&D \347\224...' did not match any files`. – Jeff Tian Mar 18 '19 at 05:34
  • I may not have copied the escape sequences correctly—they were in an image, not text. Use your computer to decode them to be sure they're correct. – torek Mar 18 '19 at 07:00
  • Thanks I'll try next time when I got the same error. Currently I just worked it around. But you answer is very helpful, thanks a lot. – Jeff Tian Mar 18 '19 at 07:42
  • Thanks I'll try next time when I got the same error. Currently I just worked it around. But you answer is very helpful, thanks a lot. – Jeff Tian Mar 18 '19 at 07:42
1
git checkout HEAD -- <filepath>

That should work

eftshift0
  • 26,375
  • 3
  • 36
  • 60
  • It shows: error: pathspec '03.08 T&D \347\224\260\346\235\260 WeeklyReport.md' did not match any file(s) known to git. I added screenshot to the question – Jeff Tian Mar 18 '19 at 02:50
0

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

git stash

Thanks for answering.

Jeff Tian
  • 5,210
  • 3
  • 51
  • 71