git - 更新遥控器 - 我需要提供遥控器名称吗?

git - updating remotes - Do I need to provide the remotes name?

由于我们内部代码托管的 url 更改,我正在编写一个脚本来更新数百个远程存储库。

其中一些存储库具有不同的远程名称(即与来源不同)。已经测试过不提供远程名称并且可以 ping 更新的远程但我不明白这种行为。

  1. 这不正常吗?
  2. 如果有 2 个或更多遥控器会怎样?

首选以定义更明确的方式更新遥控器,我尝试使用 bash 提取原始名称:

下面远程名称和地址之间的字符是什么?

$ git remote -v
origin  ssh://git@code_hosting_site.com/project_name/repo_name.git (fetch)
      ^^

感谢您的帮助。

Is this normal?

"Normal" 是一个有趣的词。很难定义这是否正常。有一个遥控器但没有将其命名为 origin 有点 不寻常,但这不是 错误,它只是 奇数.

What happens if there are 2 or more remotes?

这取决于你。 git remote -v 将列出所有这些,因此当然可以那样提取它们。您还可以使用:

git config --get-regexp 'remote\..*'

列出所有这些。您甚至可能想要添加 --local 以确保仅列出本地 .git/config 文件中定义的遥控器,以防某些疯狂的用户在她的全局配置中定义了遥控器。 (我假设您不想尝试更新任何此类条目。)

还要注意,每个遥控器都可以定义 URL 和 推送 URL。这些配置名称不区分大小写,因此在查找任何 remote.<em>remote</em>.url 和 [=50 时应折叠大小写=]remote.remote.pushurl设置"by hand"(如果你自己写代码读取一个config文件) ,但在使用 git config --get-regexpgit remote -v 时您无需担心,因为它们会为您折叠大小写。

因此,读取 git config --get-regexp 输出的 sh 或 bash 脚本可能会使用:

# fix - called with URLs that may need fixing. Argument  is the
# type (url or pushurl), argument  is the remote,  is the URL itself.
fix() {
    local urltype= remote="" url=""
    local newurl

    case  in
    ...) newurl="echo $url | sed ...";;
    *) return;;
    esac
    git config "remote.$remote.$urltype" "$newurl"
}

git config --local --get-regexp 'remote\..*' | sort | while read -r lhs rhs; do
    set -- $(echo $lhs | sed 's/\./ /g')
    case  in
    url|pushurl) fix  "" "$rhs";;
    esac
done

(在 bash 中可以更漂亮一些;上面的内容在 POSIX sh 中有效——尽管整个事情都没有经过测试。当然你需要填写 ... 部分也是如此。)

一般来说,远程名称和URLs 不应该有白色的space。我在上面使用引号来处理可能出现的情况。 (git remote 命令不允许您添加名称中有空格的遥控器,但允许在 URL 中使用 white-space。) [=65 显然无用的用法=] 是为了处理极端情况:如果 .git/config 文件非常大,我们可以尝试 运行 git config 更改 配置,同时 git config --get-regexp 仍在读取和打印 old 配置。实际上,可能不需要。