如何获取仅为最终分支的分支列表(即不是任何其他提交的父级) - 其他单词是提交树的末尾节点的分支。
我期望git branch
命令中有像--final或--tips这样的选项,但只有--merged、--no-merged和--contains选项。
如何获取仅为最终分支的分支列表(即不是任何其他提交的父级) - 其他单词是提交树的末尾节点的分支。
我期望git branch
命令中有像--final或--tips这样的选项,但只有--merged、--no-merged和--contains选项。
更新以处理任意数量的分支,任何类型的提示:
git for-each-ref --format='%(objectname)^{commit}' \
| git cat-file --batch-check='%(objectname)^!' \
| grep -v missing \
| git log --oneline --stdin
这个还是比较容易调整的,你可以通过列出那些需要的引用来限制你所寻找的提示种类,在 for-each-ref 中可以添加进一步的排除标签提示等。
2.36
中,当我运行 git show -s --oneline --decorate $( git show-branch --independent -a )
命令时,我会得到很多警告消息,显示为 "warning: ignoring origin/my/branch/name; cannot handle more than 26 refs"。有任何想法吗?(更新:git show-branch
的手册上说:"一次不能显示超过29个分支和提交")- 这就解释了这个问题,但是为什么会出现 26 和 29 的差异呢? - Dai已经有一些有用的答案,但没有给出所要求的内容:分支列表(对我来说这意味着分支名称,而不是提交ID)。
{
echo "** Below is list of tip branches a.k.a. final nodes."
echo "** These aren't reachable from any other branches."
echo "** Removing them is potentially DANGEROUS: it would make some commit unreachable."
TIPS=$( git rev-list --branches --children | grep -v ' ' | sort )
{ while read branchname commitid message
do grep $commitid < <(echo $TIPS) -q && echo $branchname
done
} < <(git branch -v --no-abbrev | sed 's/\*//' )
echo "** end of list"
}
结果看起来像这样:
** Below is list of tip branches a.k.a. final nodes.
** These aren't reachable from any other branches.
** Removing them is potentially DANGEROUS: it would make some commit unreachable.
feature-workingonit
dev
** end of list
在整理事务时,您可能想查看非尖端分支。以下是获取它们列表的方法。唯一的变化是将&&
替换为||
。
{
echo "** Below is list of non-tip branches a.k.a. inner nodes."
echo "** These are reachable from other branches."
echo "** If you remove them, all commits would stay reachable, but be careful anyway."
TIPS=$( git rev-list --branches --children | grep -v ' ' | sort )
{ while read branchname commitid message
do grep $commitid < <(echo $TIPS) -q || echo $branchname
done
} < <(git branch -v --no-abbrev | sed 's/\*//' )
echo "** end of list"
}
** Below is list of non-tip branches a.k.a. inner nodes.
** These are reachable from other branches.
** If you remove them, all commits would stay reachable, but be careful anyway.
feature-alreadymerged
master
** end of list
您可以根据需要进行装饰,例如通过更改:
echo $branchname
到达
echo -e "$branchname\t$commitid\t$message"
** Below is list of non-tip branches a.k.a. inner nodes.
** These are reachable from other branches.
** If you remove them, all commits would stay reachable, but be careful anyway.
feature-alreadymerged 426ac5186841876163970afdcc5774d46641abc4 Cool feature foo, ref task #1234
master 39148238924687239872eea425c0e837fafdcfd9 Some commit
** end of list
您可以在喜欢的工具中查看这些分支,以确定是否有必要将它们删除。
由于分支只是在工作目录中添加新提交时移动的标签 - 所有分支都“根据定义”是树的叶节点。
但我想你正在寻找那些与另一个分支的叶提交没有共同祖先的分支。因此,您需要 C
和 E
,但不需要下面的 A
、B
或 D
。
-A-o-B-o-C
\
D-o-E
-A-o-B-o-C
\
D-o-E
当以C
为HEAD
时,命令git merge --squash E
将把E
中包含的所有更改放入索引中,准备提交。提交后树的结构将如下所示:
-A-o-B-o-o-C <== notice branch `C` has moved forward one commit
\
D-o-E
C
提交现在包含了所有来自 D-o-E
的更改,没有说明它来自哪里。现在很可能不需要 D-o-E
了,但只有项目知识可以告诉你。
因为你要做的是一个手动过程,所以你需要理解。我想到了一些命令,可以帮助你确定是否可以删除分支。
这个 git log
命令产生的是一个图形,只显示修饰过的提交(分支、标签和 HEAD)。
git log --all --graph --oneline --simplify-by-decoration --decorate
>> <filename>.txt
以将图形转储到可用铅笔标记的文本文件中。<branch>
开始的祖先分支列表。这将列出已完全合并(具有合并提交)到<branch>
中的分支。git branch --merged <branch>
这可能不完全是您想要的,但正如我上面所解释的,我认为这是不可能的。希望这些命令能够帮助到您。
如果您获取所有引用的列表,您可以列出所有不是其他引用父级的引用:
refs=`git show-ref -s`
git log --oneline --decorate $refs --not `echo "$refs" | sed 's/$/^/'`
这是通过过滤掉所有的父提交来实现的。