2016-07-06 54 views
0

運行時git branch -r我看到遠程存儲庫上的分支。 有沒有辦法在同一個工作目錄中看到多個存儲庫的分支? 我的目標是創建一個列出了幾個倉庫的所有分支的文件,如:git branch for multiple remotes

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 

我只是需要與幾個倉庫的此功能工作,而無需克隆每一個人他們的這一單一目的。 謝謝。

回答

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

或作爲一個班輪沒有評論

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

您可以使用git remote add nameurl將存儲庫添加到相同的工作目錄中,然後在您執行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 
+0

很大。有關如何重新調整我的awk以實現我的最終結果的任何建議? – Moshe

+0

@Moshe:不幸的是我用awk不太流利。 – majk

+0

@Moshe只是接受我的回答,那裏有你需要的所有信息。 ;-) – Vampire

0

運行git remote add後添加所有遠程倉庫,並運行git fetch檢索遠程資源庫/更新信息,git branch -a將顯示所有分支機構,遠程和本地。

remotes/{remote_name}/{branch_name}