0

I started to learn git yesterday, and I made a mistake when I created ssh key as the first image shows.

create file img

I tried .bat delete way and dos del command, still cannot delete file named cd ..
The prompt said cannot find file. The attribute of file size is 0 byte.

file in explorer

How to delete this file?

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • wrap it in comma `rm "cd .."` OR right click and delete ;) – Vijay Agrawal Aug 08 '17 at 03:23
  • @VijayAgrawal I did that, it responded with $ git rm "cd .." fatal: pathspec 'cd ..' did not match any files – figureout77 Aug 08 '17 at 04:21
  • The Windows API defaults to normalizing filenames using quirky old rules inherited from MS-DOS. So trying to open a handle to "cd .." will instead try to open the name "cd" , with the trailing spaces and dots stripped out. The way around this preprocessing is to prefix a fully-qualified path with ``\\?\``. Try the following command in CMD: `del "\\?\D:\lernen\git\learngit\cd .."`. – Eryk Sun Aug 08 '17 at 06:24
  • @eryksun Good point. I have included that syntax (and a link to document it) in my answer, for more visibility. – VonC Aug 08 '17 at 06:52

2 Answers2

3

I managed to delete "cd .." in a CMD Windows with <path/to/git>\latest\usr\bin in %PATH%. That gives me access to rm.exe.

vonc@VONCAVN7 C:\test
> where rm
D:\prgs\git\latest\usr\bin\rm.exe

I had:

vonc@VONCAVN7 C:\test
> dir /x
 Volume in drive C is test

 Directory of C:\test

08/08/2017  07:11    <DIR>                       .
08/08/2017  07:11    <DIR>                       ..
08/08/2017  07:11                 0              cd ..

With that, I typed:

vonc@VONCAVN7 C:\test
> rm cd*

And the file cd .. was gone

As commented by eryksun,

rm.exe isn't a Linux app. It uses msys-2.0.dll, which links with Windows API functions from kernel32.dll and native NT system calls from ntdll.dll.
In this case it's how it bypasses the Windows API to make direct system calls that solves the problem: NtOpenFile (open the directory to list it and the "cd .." file to delete it), NtQueryDirectoryFile (list the directory), and NtSetInformationFile (set the file's delete disposition).


As eryksun commented, the pure Windows syntax (meaning, it does not need a Git Linux-like command like rm) would have worked too:

del "\\?\C:\test\cd .."

See "What does \\?\ mean when prepended to a file path".
That will disable all string parsing and send the string that follows it straight to the file system.

VonC
  • 1,042,979
  • 435
  • 3,649
  • 4,283
  • wowwwww! Delete it!!! VonC, thank you very much! and I want to know only git rm.exe can remove the file? because it is created by git command? – figureout77 Aug 08 '17 at 06:05
  • @figureout77 No, it was not created by a git command, but by ssh-keygen (https://linux.die.net/man/1/ssh-keygen, a linux command, packaged with Git for Windows). Since the file was not tracked by Git, `git rm` could not work. – VonC Aug 08 '17 at 06:30
  • @figureout77 I have edited the answer with an alternative which does not require a Linux command, but work just with Windows `del`. – VonC Aug 08 '17 at 06:52
  • rm.exe isn't a Linux app. It uses msys-2.0.dll, which links with Windows API functions from kernel32.dll and native NT system calls from ntdll.dll. In this case it's how it bypasses the Windows API to make direct system calls that solves the problem: `NtOpenFile` (open the directory to list it and the "cd .." file to delete it), `NtQueryDirectoryFile` (list the directory), and `NtSetInformationFile` (set the file's delete disposition). – Eryk Sun Aug 08 '17 at 11:34
  • @eryksun Good point (one I should have known, having documented/commented about msys2 for years (as in https://stackoverflow.com/a/35099458/6309). I have included your comment (with some additional links) in the answer for more visibility. – VonC Aug 08 '17 at 12:52
  • VonC and @eryksun Thank all of you, tried both way to delete the file. I have learned so much! – figureout77 Aug 09 '17 at 01:39
0

Look into the properties of the file. There you may find file location. Go to that location and delete from there.

And see which type of file is it. I mean, it may be a system file, and if it is, system will not allow you to delete it. Open explorer as administrator and then try deleting it.

Ayushya
  • 5,820
  • 3
  • 30
  • 51
  • The file is created by myself when using git command. I screenshot two images in the question description. When I try to delete it, the question is it says cannot find the file. No matter I try to delete, rename or move it. – figureout77 Aug 08 '17 at 04:24