Git is very slow in WSL

Git is very slow in wsl

Git

最近切换到 Windows 工作,使用 WSL 作为命令行方案。但在 WSL 里面安装 git 后,在工程里面执行命令时,总感觉非常缓慢。
执行一下 git status 都要卡好一会儿。

经过一番搜索,原因大概是 Windows 的路径索引到 WSL 之间有一个转换导致的?具体没有细看。

但找到一个解决方案:

先在 Windows 中安装 Git,然后在 WSL 里面用 git.exe 替代 Linux 版本的 git。

因为我使用的oh-my-zsh,所以通过在.zshrc中定义git函数针对 WSL 路径和 Windows 路径区别处理(这个代码是抄的):

1
2
3
4
5
6
7
function git() {
  if $(pwd -P | grep -q "^\/mnt\/.\/*"); then
    git.exe "$@"
  else
    command git "$@"
  fi
}

但这样做需要注意两个环境的.gitconfig不是共用的。
可以配置includeIf,通过匹配 gitdir, 在 WSL 里面引用 Windows 的 .gitconfig;又在 Windows 里面引用 WSL 的 .gitconfig。

其它

然后依葫芦画瓢:

  • 在 WSL 中以用 Windows 环境打开 VS Code (如果是用 code 命令,打开的是 WSL 环境下的):

    1
    2
    3
    
    function code_win() {
      cmd.exe /c code.cmd $(wslpath -w "$@")
    }

  • 用 open 打开资源管理器:

    1
    2
    3
    4
    
    function open() {
      explorer.exe $(wslpath -w "$@")
      return 0
    }

使用 Hugo 构建
主题 StackJimmy 设计