【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"
相关推荐
陈随易1 天前
FFmpeg 9.0 发布,代号 Lei,音视频处理再升级
前端·后端·程序员
凌虚1 天前
面向 MySQL 用户的 PostgreSQL 快速上手指南
数据库·后端·架构
IT_陈寒1 天前
Vite静态资源引用这个坑我踩得有点疼
前端·人工智能·后端
前端开发张小七1 天前
Java 学习笔记 · 第一课:基础语法与核心概念
java·后端
海盗12341 天前
微软技术周报 ——2026-08-03
后端·python·microsoft·c#·.netcore
沙湖遇雨1 天前
6. Pipeline 的生命周期
后端
青青子衿悠悠我心1 天前
TRAE Work让财务月底对账3小时变5分钟
后端
创安电气研究所1 天前
用TypeScript给Modbus设备做一层适配器
后端
妙码生花1 天前
从 PHP 到 AI + Golang,程序员自救转型手记(五十):增加管理员角色组管理
前端·后端·ai编程
妙码生花1 天前
从 PHP 到 AI + Golang,程序员自救转型手记(五十一):管理员和角色组的关联
前端·后端·ai编程