如何列出已推送到远程的分支?

时间:2021-06-21 03:17:06

I can find a lot of answers to list tracking branches (here), what I would like to do is check what local branches can be safely deleted because the commits have been pushed to a remote already.

我可以找到很多列表跟踪分支的答案(这里),我想要检查哪些本地分支可以安全删除,因为提交已经被推送到远程。

In most cases, I pushed those branches to the remote and they are not "tracking", so that part is not really helping. However in most cases, the remote branch has the same name and should point to the same commit (but not always true).

在大多数情况下,我将这些分支推到遥控器上并且它们没有“跟踪”,所以这部分并没有真正起作用。但是在大多数情况下,远程分支具有相同的名称,并且应指向相同的提交(但并非总是如此)。

Seems like this should be fairly common thing to do?

看起来这应该是相当普遍的事情吗?

1 个解决方案

#1


2  

The way I found to do this is:

我发现这样做的方式是:

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

of course, origin can be changed to the name of whatever remote.

当然,原点可以改为任何遥控器的名称。

This will output the number of remote branch that contains the local branch. If that number is different than 0, then I'm good to clean it up from my local repository.

这将输出包含本地分支的远程分支的数量。如果该数字不是0,那么我很高兴从我的本地存储库中清除它。


Update, made it into a script:

更新,使其成为一个脚本:

#!/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

#1


2  

The way I found to do this is:

我发现这样做的方式是:

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

of course, origin can be changed to the name of whatever remote.

当然,原点可以改为任何遥控器的名称。

This will output the number of remote branch that contains the local branch. If that number is different than 0, then I'm good to clean it up from my local repository.

这将输出包含本地分支的远程分支的数量。如果该数字不是0,那么我很高兴从我的本地存储库中清除它。


Update, made it into a script:

更新,使其成为一个脚本:

#!/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