Git 遇上特殊檔名難題

Dian
3 min readApr 20, 2020

今天突難遇到了一個在 windows 上 Git clone 的特殊檔名的問題。
原始的 project 應該不是在 windows 上開發的,所以很不幸的某些檔案的檔名使用了 windows 的檔案系統不接受的檔名。

而在 windows 上的檔名是不準出現一些特殊字元的,也就是保留字元
參考了這篇文章應該有以下的特殊字元

- < (less than)
- > (greater than)
- : (colon)
- " (double quote)
- / (forward slash)
- \ (backslash)
- | (vertical bar or pipe)
- ? (question mark)
- * (asterisk)

在網路上找了很多資料,大部分都是把有問題的檔名改掉,像是原檔中有出現 “:” ABC:123.jpg ,就把保留字換為 “_” 變成 ABC_123.jpg
但是我有不能亂改檔名的苦衷 ><

或是用sparse checkout 指定 checkout 的路徑來迴避有問題的檔案,但是並沒有解決 error: invalid path ’xxx’ 的問題,而且還在 powershell 遇到了編碼問題特。

總之繼續在網路上尋找解方。

解方如下:
1. 迴避檔名問題

// 迴避 NTFS 檔名問題
git config core.protectNTFS false

2. 如果是直接透過 powershell echo 路徑到 .git/info/sparse-checkout 檔案中會遇到編碼問題,可以加上out-file 來修正編碼問題

echo some/sub/folder/you/want | out-file -encoding ascii .git/info/sparse-checkout

參考資料:

  1. https://brendanforster.com/notes/fixing-invalid-git-paths-on-windows/
  2. https://stackoverflow.com/questions/23289006/on-windows-git-error-sparse-checkout-leaves-no-entry-on-the-working-directory

--

--