重装系统后GIT仓库修复

重装系统之后,打开原系统的GIT仓库会报账户所有者错误,如下图:

bash 复制代码
fatal: detected dubious ownership in repository at 'D:/Projects/xxx'%0A'D:/Projects/xxx' is owned by:%0A        (inconvertible) (S-1-5-21-2957936414-1458211769-1950632429-1001)%0Abut the current user is:%0A        XL-HP/xx (S-1-5-21-1863453967-816813999-1307148958-1001)%0ATo add an exception for this directory, call:%0A%0A        
git config --global --add safe.directory D:/Projects/xxx

解决方案:

将如下脚本另存为PowerShell脚本

bash 复制代码
# 获取当前脚本所在目录(支持相对路径与绝对路径)
$TargetDir = Split-Path -Parent $MyInvocation.MyCommand.Path

Write-Host "正在修复目录所有者:" $TargetDir -ForegroundColor Cyan

# Step 1: 使用 takeown 取得所有权(递归)
# /D Y 自动回答 Yes,避免交互阻塞
takeown /F "$TargetDir" /R /D Y

# Step 2: 使用 icacls 设置 Owner 为当前用户(递归)
# %USERNAME% 在 PowerShell 中等价于 $env:USERNAME
icacls "$TargetDir" /setowner "$env:USERNAME" /T

Write-Host "所有者修复完成。" -ForegroundColor Green

然后放到需要处理的目录下,以管理员身份运行即可,注意保存的ps1脚本编码需要设置为:UTF-8-BOM

相关推荐
嘻嘻仙人1 天前
Ubuntu中 git上传自己的项目和二次上传一般流程
git·github
Patrick_Wilson1 天前
Squash Merge 的血缘陷阱:为什么删掉的代码又活了过来
前端·git·程序员
沉浸学习的匿名网友1 天前
什么是 .gitignore?为什么每个 Git 项目几乎都离不开它?
前端·git
深海鱼在掘金2 天前
Git 完全指南 —— 第3章:理解工作区、暂存区、版本库三个核心
git
江华森2 天前
Git 基础筑基:从原理到团队协作的全栈实战
git
JakeJiang2 天前
Git 必备命令指南:从日常高频到项目开发实战
git
叫我少年3 天前
Windows 中安装 git
git
深海鱼在掘金8 天前
Git 完全指南 —— 第1章:Git 概览与版本控制演进
git
noravinsc9 天前
关于Git Flow
git
蜜獾云9 天前
在Git中配置用户名和密码
git