重装系统之后,打开原系统的GIT仓库会报账户所有者错误,如下图:
bash
fatal: detected dubious ownership in repository at 'D:/Projects/xxx'%0A'D:/Projects/xxx' is owned by:%0A (inconvertible) (S-1-5-21-2957936414-1458211769-1950632429-1001)%0Abut the current user is:%0A XL-HP/xx (S-1-5-21-1863453967-816813999-1307148958-1001)%0ATo add an exception for this directory, call:%0A%0A
git config --global --add safe.directory D:/Projects/xxx
解决方案:
将如下脚本另存为PowerShell脚本
bash
# 获取当前脚本所在目录(支持相对路径与绝对路径)
$TargetDir = Split-Path -Parent $MyInvocation.MyCommand.Path
Write-Host "正在修复目录所有者:" $TargetDir -ForegroundColor Cyan
# Step 1: 使用 takeown 取得所有权(递归)
# /D Y 自动回答 Yes,避免交互阻塞
takeown /F "$TargetDir" /R /D Y
# Step 2: 使用 icacls 设置 Owner 为当前用户(递归)
# %USERNAME% 在 PowerShell 中等价于 $env:USERNAME
icacls "$TargetDir" /setowner "$env:USERNAME" /T
Write-Host "所有者修复完成。" -ForegroundColor Green
然后放到需要处理的目录下,以管理员身份运行即可,注意保存的ps1脚本编码需要设置为:UTF-8-BOM