2014-09-04 66 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