项目文件在 Windows 的其他盘符中,此时在 wsl 中使用 git 比较卡顿,尤其项目较大时使用 git status
命令超级慢。
原因大概是 Windows 的文件到 WSL 之间系统是隔离的,相当于远程文件导致的,请见:[wsl2] filesystem performance is much slower than wsl1 in /mnt · Issue #4197 · microsoft/WSL (github.com)。
解决方案:
先在 Windows 中安装 Git,然后在 WSL 里面用 git.exe 替代 Linux 版本的 git。
在 wsl 中可以查看 git.exe 的位置 which git.exe
一、软链方式
可以给 git.exe 建立一个软链方便调用,我们用 gitw 来表示 windows 下的 git
ln -s /mnt/c/Program\ Files/Git/cmd/git.exe /usr/bin/gitw
之后就可以使用 gitw 来调用window下的 git 命令了,超级快!
二、别名方式
比如使用 gits 来表示 git status
命令,原因是我发现只有这个 status 命令肉眼可见的慢,其他命令还可以接受。而且我使用 neovim ,这个依赖Linux发行版中的 一些 git 命令。
这里直接在 .bahrc 中追加
# use wsl git
alias gits="git.exe status"
三、函数方式
主要通过函数来简单判断Linux中当前的路径,如果包含 /mnt
就使用 git.exe,反之。
参见GitHub问题中的答案: https://github.com/microsoft/WSL/issues/4401#issuecomment-670080585,如下:
# checks to see if we are in a windows or linux dir
function isWinDir {
case $PWD/ in
/mnt/*) return $(true);;
*) return $(false);;
esac
}
# wrap the git command to either run windows git or linux
function git {
if isWinDir
then
git.exe "$@"
else
/usr/bin/git "$@"
fi
}
参考:git status is slow in WSL2 · Issue #4401 · microsoft/WSL (github.com)