← 返回
未分类

batch-git-url-replace

批量替换指定目录下所有 Git 仓库的远程地址(remote URL)。 当用户需要将 Git 仓库从一个服务器迁移到另一个服务器时使用。 触发词:git remote 替换、git url 批量修改、git 仓库迁移、更换 git 地址、批量修改 remote url。
批量替换指定目录下所有 Git 仓库的远程地址(remote URL)。 当用户需要将 Git 仓库从一个服务器迁移到另一个服务器时使用。 触发词:git remote 替换、git url 批量修改、git 仓库迁移、更换 git 地址、批量修改 remote url。
zxfcn zxfcn 来源
未分类 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 0
Stars
📥 481
下载
💾 0
安装
1
版本
#latest

概述

批量替换 Git 远程地址

输入参数

执行前必须从用户处获取以下参数:

  • scanDir:扫描目录路径(如 D:\ 或 /home/user/projects)
  • oldUrl:旧 Git 服务器地址
  • newUrl:新 Git 服务器地址

执行步骤

Windows 系统

使用 PowerShell 执行以下命令(将参数替换为用户提供的实际值):

$scanDir = "<scanDir>"
$oldUrl = "<oldUrl>"
$newUrl = "<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

Linux 系统

使用 Bash 执行以下命令:

SCAN_DIR="<scanDir>"
OLD_URL="<oldUrl>"
NEW_URL="<newUrl>"

echo "开始扫描 $SCAN_DIR 下的所有 .git/config 文件..."
echo "目标:将 $OLD_URL 替换为 $NEW_URL"
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|${OLD_URL}|${NEW_URL}|g" "$config"
        echo "[已修改] $config"
        ((count_success++))
    else
        ((count_skip++))
    fi
done < <(find "$SCAN_DIR" -name ".git" -type d 2>/dev/null)

echo "----------------------------------------"
echo "处理完成!成功: $count_success, 跳过: $count_skip"

执行后

建议用户进入某个项目目录运行 git remote -v 验证是否生效。

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-30 17:56 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

dev-programming

Github

steipete
使用 `gh` CLI 与 GitHub 交互,通过 `gh issue`、`gh pr`、`gh run` 和 `gh api` 管理议题、PR、CI 运行及高级查询。
★ 679 📥 327,812
dev-programming

CodeConductor.ai

larsonreever
AI驱动平台,提供快速全栈开发、智能体、工作流自动化及低代码AI集成的可扩展产品创建。
★ 72 📥 181,970
dev-programming

Mcporter

steipete
使用 mcporter CLI 直接列出、配置、认证及调用 MCP 服务器/工具(支持 HTTP 或 stdio),涵盖临时服务器、配置编辑及 CLI/类型生成功能。
★ 195 📥 67,703