最近换了一台电脑(系统:Windows 11,Git Base 版本:2.43.0),为了方便,直接将原电脑旧硬盘挂载到了新电脑上。在旧硬盘的文件夹里执行 git 命令时,遇到如下报错:

fatal: detected dubious ownership in repository at 'D:/phpstudy_pro/WWW/www.codesou.cn'
'D:/phpstudy_pro/WWW/www.codesou.cn' is owned by:
        'S-1-5-32-544'
but the current user is:
        'S-1-5-21-3341154960-3473901704-2958967699-1001'
To add an exception for this directory, call:

        git config --global --add safe.directory D:/phpstudy_pro/WWW/www.codesou.cn

这段错误信息表示文件夹的所有者发生了变化,信息里也给出了相应的解决方案,就是为该目录添加例外,复制运行标红的命令即可。

git config --global --add safe.directory D:/phpstudy_pro/WWW/www.codesou.cn

再次执行 git 命令,顺利执行。

附:我是按照上述方法就直接解决问题了,如果添加例外后仍然报该错误,那么就只能更改目录的所有者了。更改方法如下:

  • 右击文件夹,点击属性
  • 进入安全选项卡,点击高级
  • 进入高级,修改所有者为当前登录账号