Questions & Answers

How to "git restore --staged ." the deleted files/folder with a colon (:) in their directory name on windows?

I have a git repository folder on a usb taken from a linux machine.

  • That repo had two folders with a colon (:) in their folder name (the linux distro allowed those)
  • All the folders in the repo were deleted and this deletion was then staged but not committed.
  • This repo was then copied to the aforementioned usb.
  • I am now on a windows machine and would like to restore the deleted files/folders.

I tried to use these commands:

  1. "git restore ." which restored all the files which were not having a : in their name or their folder name.
  2. "git restore --staged ." which give me this error: "error: invalid path '<foldernamewitha:>/<filename>.<ext>'"

I guess this is because windows does not allow : in names of file/folders. Is there any way around this?

Answers(1) :

Use an OS that permits : in filesystem paths or rename the paths. Them's your options.

If you're stuck on Windows you can brute-force this with core commands, find a workable replacement for the colons. Perhaps you could use url encoding?

git ls-tree -r @ | sed 's,:,%3A,g' | git update-index --index-info

index-info docs

but you'd then have to reverse the process on OS's without those limitations, and no code looking for the unmangled names would find these files, so this only gets you one more step along the way to whatever you're trying to do. It's possible "just don't use Windows for whatever this is" would be best, also possible that's not an option; if this step doesn't get you there you're going to have to explain what you're trying to do here.

edit: one possibility: are you trying to reset just the paths that don't have colons in them? s,:,%3A,g to /:/d in the above sed.

2023-01-24 00:00:04
Yeah figured, I'll have to sort this out in an OS that allows : in names. Thanks for help!