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