重装了Windows系统,导致用户的id发生了变更。 由于Windows的安全策略,在执行git命令的时候,提示了下面的问题。
fatal: detected dubious ownership in repository at xxxx
对策
- 一键脚本(保存为
Take-Ownership.ps1
)
powershell
<#
.SYNOPSIS
递归取得目录所有权并赋权给当前用户
.PARAMETER Path
要处理的目录
.EXAMPLE
.\Take-Ownership.ps1 -Path "D:\MyFolder"
#>
param(
[Parameter(Mandatory=$true)]
[ValidateScript({Test-Path $_})]
[string]$Path
)
# 当前登录用户的 SID(形如 S-1-5-21-...-1001)
$mySid = ([System.Security.Principal.WindowsIdentity]::GetCurrent()).User.Value
$userName = "$env:USERDOMAIN\$env:USERNAME"
# 支持超长路径
function Get-FullPath([string]$p) {
$full = [System.IO.Path]::GetFullPath($p)
if ($full.Length -ge 248) { $full = "\\?\$full" }
return $full
}
$fullPath = Get-FullPath $Path
Write-Host "正在取得所有权并赋权:$fullPath" -ForegroundColor Cyan
# 1) takeown 递归拿所有权
takeown /f $fullPath /r /d Y | Out-Null
# 2) icacls 赋权(递归、替换继承)
icacls $fullPath /grant "${userName}:(OI)(CI)F" /T /C /Q
Write-Host "完成!" -ForegroundColor Green
- 执行脚本
powershell
.\take_ownership.ps1 -Path "D:\Your\Folder"
如果运行遇到问题,可以尝试:
powershell
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass # 仅当前会话允许脚本
powershell
.\Take-Ownership.ps1 -Path "D:\Your\Folder"