Git 解决安全目录限制

  • 重装系统后,在原来的项目下执行 git 命令时提示:

    fatal: detected dubious ownership in repository at 'D:/Projects/xxxxx'
    'D:/Projects/xxxxx' is owned by:
            'S-1-5-21-2440756236-1610524770-2949854643-1001'
    but the current user is:
            'S-1-5-21-2927613386-97785579-904096897-1001'
    To add an exception for this directory, call:
    
            git config --global --add safe.directory D:/Projects/xxxxx
    
  • 原因是 Git 出于安全方面考虑,在某个版本新增了安全目录的限制,所以可以按照提示执行 git config --global --add safe.directory D:/Projects/xxxxx​ 将对应项目的路径添加到安全目录里。

  • 如果是个人电脑,一个项目一个项目的添加实在是过于麻烦,此时可以使用通配符忽略所有目录:git config --global --add safe.directory "*"

  • 关闭所有终端,重新打开后应该可以正常执行 git 命令了。