Git下文件(夹)时间全部错误更新的恢复方法

1. 让 Git 允许输出中文(一劳永逸)

1
git config --global core.quotepath false

2. 运行恢复脚本

直接把下面这段完整的代码再次复制粘贴进去运行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 强制指定管道和输出皆为 UTF-8 编码,彻底打通中文路径
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
$OutputEncoding = [System.Text.Encoding]::UTF8

# 重新获取文件并恢复时间
git ls-files | ForEach-Object {
$file = $_
# 直接尝试获取 Git 时间,如果文件存在且有历史,就直接修改
$gitTimeStr = git log -1 --format=%cd --date=iso $file
if ($gitTimeStr -and (Test-Path -LiteralPath $file)) {
$gitTime = [DateTime]::Parse($gitTimeStr)
# 使用 -LiteralPath 阻止 PowerShell 把中文或特殊符号误认为通配符
Set-ItemProperty -LiteralPath $file -Name CreationTime -Value $gitTime
Set-ItemProperty -LiteralPath $file -Name LastWriteTime -Value $gitTime
Set-ItemProperty -LiteralPath $file -Name LastAccessTime -Value $gitTime
Write-Host "已完美恢复中文文件时间: $file -> $gitTimeStr" -ForegroundColor Green
}
}

在要批量更改创建时间的文件夹下进入powershell

image-20260708175303785