2014-09-04 65 views
2

我可以找到很多列表跟蹤分支的答案(here),我想要做的是檢查哪些本地分支可以安全地刪除,因爲提交已被推送到遠程已經。如何列出已推送至遠程的分支?

在大多數情況下,我將這些分支推送到遠程,並且它們不是「跟蹤」的,所以這部分並沒有真正的幫助。但是,在大多數情況下,遠程分支具有相同的名稱,並且應該指向相同的提交(但不總是爲真)。

看起來像這樣應該是相當普遍的事情嗎?

+1

這不是一個自動的解決方案,但你總是可以運行,爲每個遠程分支,'的Git分支--merged產地/ remotebranch'到列出可從「remotebranch」頭部到達的本地分支。 – Jubobs 2014-09-04 22:53:36

+0

謝謝@Jubobs,這讓我走上了正確的道路...... – Matthieu 2014-09-05 08:09:15

回答

1

我發現做到這一點的方法是:

git branch -a --contains name_of_local_branch | grep -c remotes/origin 

當然,origin可以改爲無論遙控器的名稱。

這將輸出包含本地分支的遠程分支的數量。如果這個數字不是0,那麼我很好從我的本地存儲庫中清理它。


更新,把它做成一個腳本:

#!/bin/bash 
# Find (/delete) local branches with content that's already pushed 
# Takes one optional argument with the name of the remote (origin by default) 

# Prevent shell expansion to not list the current files when we hit the '*' on the current branch 
set -f 

if [ $# -eq 1 ] 
then 
    remote="$1" 
else 
    remote="origin" 
fi 

for b in `git branch`; do 

    # Skip that "*" 
    if [[ "$b" == "*" ]] 
    then 
    continue 
    fi 

    # Check if the current branch tip is also somewhere on the remote 
    if [ `git branch -a --contains $b | grep -c remotes/$remote` != "0" ] 
    then 
    echo "$b is safe to delete" 
    # git branch -D $b 
    fi 
done 

set +f