【GiraKoo】Windows的目录ownership问题

重装了Windows系统,导致用户的id发生了变更。 由于Windows的安全策略,在执行git命令的时候,提示了下面的问题。

fatal: detected dubious ownership in repository at xxxx

对策

  1. 一键脚本(保存为 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
  1. 执行脚本
powershell 复制代码
.\take_ownership.ps1 -Path "D:\Your\Folder"

如果运行遇到问题,可以尝试:

powershell 复制代码
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass   # 仅当前会话允许脚本
powershell 复制代码
.\Take-Ownership.ps1 -Path "D:\Your\Folder"
相关推荐
M1A19 小时前
小红书重磅升级!公众号文章一键导入,深度内容轻松入驻
后端
0wioiw010 小时前
Go基础(④指针)
开发语言·后端·golang
李姆斯12 小时前
复盘上瘾症:到底什么时候该“复盘”,什么时候不需要“复盘”
前端·后端·团队管理
javachen__12 小时前
Spring Boot配置error日志发送至企业微信
spring boot·后端·企业微信
seabirdssss12 小时前
使用Spring Boot DevTools快速重启功能
java·spring boot·后端
OC溥哥99914 小时前
Flask论坛与个人中心页面开发教程完整详细版
后端·python·flask·html
迷知悟道15 小时前
java面向对象四大核心特征之抽象---超详细(保姆级)
java·后端
Aurora_NeAr16 小时前
对比Java学习Go——程序结构与变量
后端
AntBlack16 小时前
每周学点 AI:ComfyUI + Modal 的一键部署脚本
人工智能·后端·aigc
5大大大大雄17 小时前
docker容器日志处理
后端