【问题标题】:How do I list branches that have been pushed to a remote?如何列出已推送到远程的分支?
【发布时间】:2014-10-29 19:12:17
【问题描述】:

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

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

似乎这应该是相当普遍的事情?

【问题讨论】:

  • 这不是一个自动解决方案,但您始终可以为每个远程分支运行 git branch --merged origin/remotebranch 以列出可以从 remotebranch 的头部访问其提示的本地分支。
  • 感谢@Jubobs,这让我走上了正轨……

标签: git git-branch git-remote


【解决方案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

【讨论】:

    猜你喜欢
    • 2020-05-22
    • 2011-09-10
    • 2016-07-08
    • 2017-04-16
    • 2018-12-22
    • 2011-06-09
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    相关资源
    最近更新 更多