Skip to content

建立 2026-09-14 更新 2026-09-14

Git

在 WSL 裡把 Git 當成 Linux 工具 來用:儲存庫放 Linux 檔案系統,git 也在發行版裡安裝。不要用 Windows 的 Git 去操作 \\wsl$ 底下的同一個工作樹,行尾與檔案監控都容易出怪問題。

安裝與基本設定

sudo apt update
sudo apt install git
git config --global user.name "Your Name"
git config --global user.email "you@example.com"

行尾(CRLF/LF)建議在 WSL 維持 Linux 慣例:

git config --global core.autocrlf input

Windows 版 Git 常用 true(提交時轉 LF、取出時轉 CRLF)。兩邊策略不一致時,同一檔會一直出現「整份被改過」。選一邊當主要工作環境即可。

認證

HTTPS 可把 Windows 的 Git Credential Manager 接過來,避免在 Linux 裡另存一套密碼。官方環境建議見 Set up Git in WSL。常見作法是在 WSL 設定:

git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/bin/git-credential-manager.exe"

實際路徑依你的 Git for Windows 安裝位置調整。若尚未安裝 Git for Windows,也可只用 SSH。

SSH 金鑰在 WSL 家目錄 產生(~/.ssh),不要複製 Windows 使用者目錄裡那一份除非你很清楚權限:

ssh-keygen -t ed25519 -C "you@example.com"
cat ~/.ssh/id_ed25519.pub

把公鑰貼到 GitHub/GitLab。私鑰權限應為僅自己可讀(chmod 600 ~/.ssh/id_ed25519)。

效能

git status 若慢得不正常,幾乎都是儲存庫放在 /mnt/c。搬到 ~/projects 再試。這比調 core.untrackedCache 有效得多。