首页 > 解决方案 > Find out if integration branch contains tip of local branches

问题描述

I want to get a list of branches I can safely delete. My question is, is there a way to determine if remotes/origin/dev contains a commit, without checking out that branch?

git fetch origin;
git branch | while read branch; do
  if [ "remotes/origin/dev" --contains "$b" ]; then
     echo "$b";
  fi
done

How to check if remotes/origin/dev contains the latest commit of a given branch? Can I avoid checking out remotes/origin/dev first?

标签: gitgit-branch

解决方案


利用:

if git merge-base --is-ancestor $b refs/remotes/origin/dev; then
    echo $b
fi

但不要git branch用于这个!改用git for-each-ref

git fetch origin
git for-each-ref --format='%(refname)' refs/heads | while read b; do
    git merge-base --is-ancestor $b refs/remotes/origin/dev && echo ${b#refs/heads/}
done

例如。(这对所有内容都使用全名,以防万一,但仍会发出缩短的名称。)


问题:

  • 分支 B 是否包含提交 C?

(对于某些分支名称/标识符B,包括远程跟踪名称和提交哈希或其他标识符C)实际上是git merge-base命令回答的另一个更一般的问题的子集。

更普遍的问题是:

  • 提交 C1 是提交 C2 的祖先吗?

其中“祖先”允许平等。这是git merge-base --is-ancestor C1 C2执行的测试。请注意,这些项目中的任何一项都可能是哈希 ID 或引用名称,结果是退出状态的一部分(足够恰当)。该--is-ancestor测试是 Git 1.8.0 中的新测试。


推荐阅读