如何从目录中的所有存储库中递归删除损坏的git remote?

时间:2018-08-03 21:23:46

标签: git git-remote

我有一个文件夹,其中充满了旧的git存储库,位于任意文件夹深度。其中许多回购协议是在我使用不再存在的旧git主机时创建的。我需要找到所有引用该旧远程服务器的本地存储库,并将其从我的本地存储库中删除。

1 个答案:

答案 0 :(得分:0)

Linux:

find . -name .git -type d -prune -exec sh -c 'git --git-dir "{}" remote -v | grep bad.domain | cut -f1 | uniq | xargs -r -n 1 git --git-dir "{}" remote rm' \;

Windows PowerShell:

find . -name .git -type d -prune -exec sh -c 'git --git-dir ''{}'' remote -v | grep bad.domain | cut -f1 | uniq | xargs -r -n 1 git --git-dir ''{}'' remote rm' ";"

Windows中的转义序列很奇怪...


这是如何工作的:

  1. find当前目录中的所有git repos(查找.git文件夹)
    • -prune阻止find递归到找到的.git目录中,从而提高了性能
  2. 对于找到的每个存储库,提取远程git ... remote -v
  3. grep代表bad.domain
  4. cut直到远程名称
  5. 使用uniq过滤重复的远程名称
  6. 使用xargs为找到的每个遥控器运行git remote rm
    • -r表示如果在该存储库上未发现任何不良的远程操作,请不要运行git remote rm
    • -L 1意味着对存储库中的每个不良远程运行一次git remote rm