【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 小时前
Gradle 基础篇之基础知识的介绍和使用
后端·gradle
Java猿_1 小时前
Spring Boot 集成 Sa-Token 实现登录认证与 RBAC 权限控制(实战)
android·spring boot·后端
小王师傅662 小时前
【轻松入门SpringBoot】actuator健康检查(上)
java·spring boot·后端
码事漫谈2 小时前
C++高并发编程核心技能解析
后端
码事漫谈2 小时前
C++与浏览器交织-从Chrome插件到WebAssembly,开启性能之门
后端
源代码•宸3 小时前
goframe框架签到系统项目(BITFIELD 命令详解、Redis Key 设计、goframe 框架教程、安装MySQL)
开发语言·数据库·经验分享·redis·后端·mysql·golang
⑩-3 小时前
SpringCloud-Nacos 配置中心实战
后端·spring·spring cloud
java1234_小锋4 小时前
[免费]SpringBoot+Vue勤工助学管理系统【论文+源码+SQL脚本】
spring boot·后端·mybatis·勤工助学
踏浪无痕5 小时前
从 Guava ListenableFuture 学习生产级并发调用实践
后端·面试·架构