shell脚本自动更正git项目url

需求

服务器修改了git工程的group后,group下的所有git地址都变了,导致本地的git远法正常更新/提交代码。
执行git pull后会出错

GitLab: Project 'old/project' was moved to 'new/project'.

Please update your Git remote and try again:

  git remote set-url origin ssh://xxxxx/new/project.git
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

比如android项目用repo来管理, 每个目录用一个子git管理,总共会有30来个git,远程修改group后会影响所有git,每个都手动去修改
比较麻烦。

用脚本来修复方便许多

实现

pullall.sh, 此脚本遍历当前目录下所有的git工程,并执行git pull命令,如果检测到出错并是url变化导致,则自动执行git remote set-url xxx命令来修复,
修复完再执行一次git pull

#!/bin/bash

curdir=`pwd`

for dir in `ls`
do
    if [ -d $dir ]; then
        echo "-----$dir----"
        cd $dir
        if [ -d .git ]; then
            repair_url=`git pull 2>&1 | grep 'git remote set-url'`
            if [ ! "$repair_url" == "" ]; then
                echo "---repair:$repair_url--"
                $repair_url
                echo "git pull"
                git pull
            fi
        fi
    fi  
    cd $curdir
done

作者:帅得不敢出门

猜你喜欢

转载自blog.csdn.net/zmlovelx/article/details/128591134
今日推荐