Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

git clone succeeded, but checkout failed; due to folders having leading or trailing spaces

Tags:

git

windows

macos

I usually commit to a project from a macOS, and I didn't notice that leading and trailing space were embedded accidentally in folder names, but lately I tried to clone the repo from Windows, I get this error:

fatal: cannot create directory at 'FolderName /SubFolderName'
warning: Clone succeeded, but checkout failed.

Is there a way to checkout successfully from windows without modifying from the mac? What to do to prevent Leading and trailing spaces that cause checkout failure in Windows? Is there a way to force Finder to highlight all leading or trailing spaces in macOS ?, or even better: reject them for compatibility purposes?

like image 707
Muhammad Annaqeeb Avatar asked Aug 30 '25 18:08

Muhammad Annaqeeb


1 Answers

Easiest would be to rename them in MacOS. But you can fix it in windows as well, using low-level commands:

  • Use sparse checkout to skip the broken path from checkout
  • Find out the dir's tree hash: git ls-tree HEAD:<parent dir> or git ls-tree HEAD if the directory is in toplevel, it would print something like "040000 tree df2b8fc99e1c1d4dbc0a854d9f72157f1d6ea078 invalid_dir"
  • Add the tree as new name: git update-index --add --cacheinfo 040000,df2b8fc99e1c1d4dbc0a854d9f72157f1d6ea078,valid_dir
  • Remove the older directory: git rm -r --cached 'invalid_dir '
  • Commit the rename: git commit -m 'Rename invalid direct'
  • Update worktree to checkout the valid directory: git reset --hard (NOTE: I assume you have not any work done yet in this instance, so there is nothing to lose)
like image 116
max630 Avatar answered Sep 02 '25 09:09

max630