如何在Bash中更新多个git存储库的远程URL?

问题描述 投票:1回答:4

我有一堆git repos已移至另一台主机。我可以使用

为单个仓库更新遥控器

git remote set-url origin <url>

我如何自动执行数十个存储库?基本上,我需要替换网址的主机名/路径部分。

bash git git-remote
4个回答
2
投票
假设所有回购都在old.example.com上并移至new.example.com,并且您当前位于包含所有回购的父目录中:

find . -name ".git" -exec sed -i 's/old\.example\.com/new\.example\.com/g' {}/config \;

这将找到所有存储库(带有.git目录),然后在config文件的每一行上用新路径替换旧路径。


1
投票
cd parent_dir && for repo in *; do cd $repo && remote=`git remote get-url origin` && remote=`echo $remote | sed s/oldhost/newhost/` && git remote set-url origin $remote && cd .. # back to parent done

1
投票
for r in *; do git -C "$r" remote set-url origin "$(git -C "$r" remote get-url origin | sed s/old/new/)" done

git -C <dir>告诉Git在执行其他操作之前先进入repo目录。然后读取当前的远程URL并使用sed进行替换。不用说,在开始弄乱Git存储库的配置之前,最好进行备份。


0
投票
find . -name ".git" -exec sed -i '' -e 's/old\.example\.com/new\.example\.com/g' {}/config \;

它为我工作,我从invalid command code ., despite escaping periods, using sed处获得了它>

© www.soinside.com 2019 - 2024. All rights reserved.