2013-03-17 75 views
3

我有一個比較大的收集遠程分支機構的從舊的遠程回購:如何批量刪除多餘的遠程git分支?

$ git branch -r 
    guy/feat1 
    guy/feat2 
    guy/feat3 
    guy/feat4 
    guy/feat5 
    guy/feat6 
    guy/feat7 
    guy/feat8 
    origin/HEAD 
    origin/master 

是否有一個命令,將刪除所有guy分支機構?

回購不再有guy作爲遠程回購。

回答

4
git branch -r | grep guy/ | xargs git branch -d 

(假設$徵兆都沒有實際的輸出的一部分...)

+0

我接受這是正確的答案,因爲它看起來工作。但是,我遇到了更大的麻煩。當運行這個命令時,我收到了錯誤消息:分支'guy/feat1'找不到.'。即使將'-d'改爲'-D'也沒有幫助。最終,下面的答案**爲我工作:http://stackoverflow.com/a/11050880/248220 – eoinoc 2013-03-17 15:57:08

+0

哇,很奇怪..... – Thomas 2013-03-17 15:58:46

+1

更像Git這樣迭代的方式是'git for- each-ref --format ='%(refname)''refs/remotes/guy/*'| xargs git branch -d' – kostix 2013-03-18 07:54:58

4

您也可以嘗試(從git remote):

git remote --prune guy 

有了:

prune 

Deletes all stale remote-tracking branches under <name> .
These stale branches have already been removed from the remote repository referenced by <name> , but are still locally available in "remotes/".

With --dry-run option, report what branches will be pruned, but do not actually prune them.

參見「Difference between git remote prune and git branch -d -r

如果男人不再是一個的Valide遠程回購,則:

git gc --prune=now 

將清理這些分支(有一些未引用提交一起,所以要小心使用它)
見更多在「How do you Remove an Invalid Remote Branch Reference from Git?」:如果可能,通常更安全:git branch -rd guy/badbranch,但如果這不起作用,那麼git gc --prune=now也可以是解決方案。

+0

當'guy'不再可以遠程使用時,我看起來像這個命令不相關。運行命令的結果是:'致命的:'人'似乎不是一個git存儲庫。 致命:遠端意外掛斷'。我想邏輯是命令去檢查哪些分支不再在那個遠程回購。 – eoinoc 2013-03-17 15:47:04

+0

@eoinoc好的,回答編輯。 – VonC 2013-03-17 15:50:29

+0

我已經運行'git gc -prune = now'。它看起來像壓縮了回購,但遠程分支仍然存在。 – eoinoc 2013-03-17 15:55:27

0

要刪除遠程分支,您可以使用 git push <remote-repo> :branch-to-delete 請注意分支要刪除之前的冒號。另請參閱Delete multiple remote branches in git ...這顯示瞭如何構建一次刪除多個分支的單行程。