【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 小时前
分布式系统高可用性设计 - 监控与日志系统
后端
songroom2 小时前
【转】Rust: PhantomData,#may_dangle和Drop Check 真真假假
开发语言·后端·rust
红尘散仙2 小时前
Rust 终端 UI 开发新玩法:用 Ratatui Kit 轻松打造高颜值 CLI
前端·后端·rust
mldong2 小时前
mldong-goframe:基于 GoFrame + Vben5 的全栈快速开发框架正式开源!
vue.js·后端·go
canonical_entropy3 小时前
集成NopReport动态生成复杂Word表格
后端·低代码
come112343 小时前
Go 包管理工具详解:安装与使用指南
开发语言·后端·golang
绝无仅有4 小时前
OSS文件上传解析失败,错误:文件下载失败的排查与解决
后端·面试·架构
LaoZhangAI4 小时前
Kiro vs Cursor:2025年AI编程IDE深度对比
前端·后端
brzhang5 小时前
OpenAI 7周发布Codex,我们的数据库迁移为何要花一年?
前端·后端·架构