【问题标题】:git branch for multiple remotes多个遥控器的 git 分支
【发布时间】:2016-11-08 09:55:59
【问题描述】:

运行git branch -r 时,我看到远程存储库上的分支。 有没有办法在同一个工作目录中查看多个存储库的分支? 我的目标是创建一个文件,列出几个存储库中的所有分支,如下所示:

repo1:master,dev,qa,fy-2473
repo2:master,dev,fy-1128,staging
repo3:master,fy-1272,staging

如此等等。 我有这个以正确的方式打印分支:

git branch -r | awk -F' +|/' -v ORS=, '{if($3!="HEAD") print $3}' >> repolist.txt

我只需要让这个功能与几个存储库一起工作,而不必为了这个单一目的而克隆每个存储库。 谢谢。

【问题讨论】:

    标签: git git-branch gawk git-remote


    【解决方案1】:

    使用git remote add 将您的存储库作为远程库添加到您的本地存储库,然后使用git fetch --all 它们并调整您的 awk 命令以产生您想要的结果。

    这个命令会产生你期望的输出

    git branch -r | awk '
        # split remote and branch
        {
            remote = substr($1, 0, index($1, "/") - 1)
            branch = substr($1, index($1, "/") + 1)
        }
    
        # eliminate HEAD reference
        branch == "HEAD" { next }
    
        # new remote found
        remote != lastRemote {
            # output remote name
            printf "%s%s:", lastRemote ? "\n" : "", remote
            lastRemote = remote
            # do not output next comma
            firstBranch = 1
        }
    
        # output comma between branches
        !firstBranch { printf "," }
        firstBranch { firstBranch = 0 }
    
        # output branch name
        { printf branch }
    
        # final linebreak
        END { print "" }
    '
    

    或者作为没有 cmets 的单线

    git branch -r | awk '{ remote = substr($1, 0, index($1, "/") - 1); branch = substr($1, index($1, "/") + 1) } branch == "HEAD" { next } remote != lastRemote { printf "%s%s:", lastRemote ? "\n" : "", remote; lastRemote = remote; firstBranch = 1; } !firstBranch { printf "," } firstBranch { firstBranch = 0 } { printf branch } END { print "" }'
    

    【讨论】:

      【解决方案2】:

      运行git remote add 添加所有远程存储库,运行git fetch 检索/更新远程存储库信息后,git branch -a 将显示所有分支,包括远程和本地。对于远程分支,它将以如下格式显示:

      remotes/{remote_name}/{branch_name}
      

      【讨论】:

        【解决方案3】:

        您可以使用git remote add name url 将存储库添加到同一工作目录,然后您将在使用git branch -r 时看到所有这些。

        例如:

        git remote add repo1 http://github.com/example/foo.git
        git remote add repo2 http://bitbucket.com/example/bar.git
        git fetch --all
        git branch -r
        

        将列出:

        repo1/master
        repo1/dev
        repo2/master
        repo2/featureXYZ
        

        【讨论】:

        • 太棒了。关于如何重新调整我的 awk 以实现我的最终结果的任何建议?
        • @Moshe:不幸的是我对 awk 不是很流利。
        • @Moshe 只需接受我的回答,您就拥有了所需的所有信息。 ;-)
        猜你喜欢
        • 1970-01-01
        • 2018-06-25
        • 2018-12-29
        • 2011-05-16
        • 2018-12-12
        • 2017-08-18
        • 1970-01-01
        • 1970-01-01
        • 2022-06-23
        相关资源
        最近更新 更多