【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"
相关推荐
h***04773 小时前
SpringBoot(7)-Swagger
java·spring boot·后端
v***91304 小时前
Spring boot创建时常用的依赖
java·spring boot·后端
Cosolar7 小时前
银河麒麟 / aarch64 系统:Docker + Docker Compose 完整安装教程
后端·程序员·架构
星释7 小时前
Rust 练习册 100:音乐音阶生成器
开发语言·后端·rust
kaliarch7 小时前
2025年IaC生态全景与实践指南:从工具选型到多云治理
后端·云计算·自动化运维
Coder-coco7 小时前
个人健康管理|基于springboot+vue+个人健康管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·mysql·论文
b***65327 小时前
springboot整合mybatis-plus(保姆教学) 及搭建项目
spring boot·后端·mybatis
5***E6857 小时前
Spring Boot与MyBatis
spring boot·后端·mybatis
x***01067 小时前
SpringSecurity+jwt实现权限认证功能
android·前端·后端
5***26228 小时前
Spring Boot问题总结
java·spring boot·后端