blog icon indicating copy to clipboard operation
blog copied to clipboard

批量替换git本地仓库远程地址/IP

Open diamont1001 opened this issue 6 months ago • 0 comments

公司的私有化部署的 gitlab 是用 IP 地址访问的,最近搬了机房导致 IP 变了,本地代码就需要换远程仓库的 IP 地址了。于是写了个脚本,批量替换本地仓库的远程地址 / IP。

假如环境如下:

  • 旧 IP 地址:111.222.333.444
  • 新 IP 地址:555.666.777.888
# 进去包含所有Git仓库的父目录
cd all_projects

# [macOS]批量替换 IP
find . -type f -name config -exec sed -i '' 's/111\.222\.333\.444/555\.666\.777\.888/g' {} +

# [Linux]批量替换 IP
find . -name config -exec sed -i 's/111\.222\.333\.444/555\.666\.777\.888/g' {} +

替换完成后,可以检查下远程仓库 URL:

# 随便进去一个仓库目录
cd project_1

# 查看仓库 URL
git remote -v

也可以在替换之前,先运行以下命令查看所有需要替换的文件:

find . -type f -name config

diamont1001 avatar Jan 03 '24 03:01 diamont1001