Moshe
Moshe

Reputation: 5139

git branch for multiple remotes

When running git branch -r I see the branches on my remote repository. Is there a way to see in the same working directory, the branches of multiple repositories? My goal is to create a file that lists all branches in couple of repositories, like this:

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

So on and so forth. I have this to print the branches the right way:

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

I just need to have this functionality work with couple of repositories without having to clone each and everyone of them for this single purpose. Thanks.

Upvotes: 0

Views: 1857

Answers (3)

Vampire
Vampire

Reputation: 38669

Add your repos as remotes to your local repo with git remote add, then git fetch --all them and adapt your awk command to produce the result you want.

This command will produce the output you expect

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 "" }
'

or as one-liner without comments

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 "" }'

Upvotes: 1

gzh
gzh

Reputation: 3596

After you run git remote add to add all remote repositories, and run git fetch to retrieve / update information of remote repositories, git branch -a will show all branches, both remote and local. For remote branches, it will be shown in format as:

remotes/{remote_name}/{branch_name}

Upvotes: 0

majk
majk

Reputation: 857

You can add the repositories to the same working directory using git remote add name url, then you will see all of them when you do git branch -r.

For instance:

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

will list:

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

Upvotes: 2

Related Questions