|
执行前必须从用户处获取以下参数:
使用 PowerShell 执行以下命令(将参数替换为用户提供的实际值):
powershell
$scanDir =
$oldUrl =
$newUrl =
Write-Host 开始扫描 $scanDir 下的所有 .git/config 文件... -ForegroundColor Cyan
Write-Host 目标:将 $oldUrl 替换为 $newUrl -ForegroundColor Cyan
Write-Host ----------------------------------------
$gitDirs = Get-ChildItem -Path $scanDir -Recurse -Directory -Filter .git -Force -ErrorAction SilentlyContinue
$countSuccess = 0
$countSkip = 0
foreach ($gitDir in $gitDirs) {
$configPath = Join-Path $gitDir.FullName config
if (Test-Path $configPath) {
try {
$content = Get-Content -Path $configPath -Raw -Encoding UTF8
if ($content -like $oldUrl) {
$newContent = $content -replace [regex]::Escape($oldUrl), $newUrl
Set-Content -Path $configPath -Value $newContent -Encoding UTF8 -NoNewline
Write-Host [已修改] $configPath -ForegroundColor Green
$countSuccess++
} else {
$countSkip++
}
} catch {
Write-Host [错误] 无法处理 $configPath : $_ -ForegroundColor Red
}
}
}
Write-Host ----------------------------------------
Write-Host 处理完成!成功: $countSuccess, 跳过: $countSkip -ForegroundColor Yellow
使用 Bash 执行以下命令:
bash
SCAN_DIR=
OLD_URL=
NEW_URL=
echo 开始扫描 $SCAN_DIR 下的所有 .git/config 文件...
echo 目标:将 $OLDURL 替换为 $NEWURL
echo ----------------------------------------
count_success=0
count_skip=0
while IFS= read -r gitdir; do
config=$gitdir/config
if [ -f $config ] && grep -q $OLD_URL $config 2>/dev/null; then
sed -i s|${OLDURL}|${NEWURL}|g $config
echo [已修改] $config
((count_success++))
else
((count_skip++))
fi
done < <(find $SCAN_DIR -name .git -type d 2>/dev/null)
echo ----------------------------------------
echo 处理完成!成功: $countsuccess, 跳过: $countskip
建议用户进入某个项目目录运行 git remote -v 验证是否生效。
该技能支持在以下平台通过对话安装:
帮我安装 SkillHub 和 batch-git-url-replace-1776201068 技能
设置 SkillHub 为我的优先技能安装源,然后帮我安装 batch-git-url-replace-1776201068 技能
skillhub install batch-git-url-replace-1776201068
文件大小: 2 KB | 发布时间: 2026-4-17 14:11