Git 获取最终分支的列表(这是最终提示)
我如何才能得到一个只属于最终分支(不是任何其他提交的父级)的分支列表——换句话说,分支是提交树(最终节点)的提示。Git 获取最终分支的列表(这是最终提示),git,tree,branch,hierarchy,git-branch,Git,Tree,Branch,Hierarchy,Git Branch,我如何才能得到一个只属于最终分支(不是任何其他提交的父级)的分支列表——换句话说,分支是提交树(最终节点)的提示。 我希望在git branch命令中有一些选项,如--final或--tips,但只有--merged,-no merged和--contains选项。因为分支只是在工作目录中活动的新提交添加时移动的标签,所以所有分支都是树的“按定义”叶节点 但我认为你是在要求那些与另一个分支的叶子没有共同祖先的分支。这样您就需要C和E,而不是A、B或D -A-o-B-o-C \
我希望在
git branch
命令中有一些选项,如--final或--tips,但只有--merged,-no merged和--contains选项。因为分支只是在工作目录中活动的新提交添加时移动的标签,所以所有分支都是树的“按定义”叶节点
但我认为你是在要求那些与另一个分支的叶子没有共同祖先的分支。这样您就需要C
和E
,而不是A
、B
或D
-A-o-B-o-C
\
D-o-E
如果是这种情况,那么就有可能找到一个列表,其中列出了所有不共享一个共同祖先(另一个分支的叶提交)的分支。例如,D
和E
共享D
当前叶提交的共同祖先
这可能是通过脚本实现的,但我不知道有哪个git命令可以为所有分支实现这一点。但是git在过去让我惊讶的是,它可以通过组合(有时是模糊的)命令参数来完成什么
在你的评论之后,我做了更多的思考,我想我更好地理解了为什么你的要求作为命令是不可能的(或者可能是有用的)。这是因为像
gitmerge--squash这样的命令不会创建合并提交。创建的提交只是另一个常规提交——不包含任何表示它来自另一个分支的内容
例如,从以下内容开始:
-A-o-B-o-C
\
D-o-E
使用HEAD
atC
命令git merge--squash E
将把E
中包含的所有更改放在索引中准备提交。一旦投入使用,这棵树看起来就像
-A-o-B-o-o-C <== notice branch `C` has moved forward one commit
\
D-o-E
运行此命令将为您提供一个视觉效果,您可以快速查看“端点”分支的位置。提示:添加>.txt
将图形转储到可以用铅笔标记的文本文件中
此外,一旦确定了要保留的“端点”分支,请运行以下命令查看
中的分支的存储列表。下面列出了完全合并(合并提交)到
中的分支
git分支——合并
这可能不是你想要的——但正如我上面解释的,我认为这是不可能的。希望这些命令能有所帮助。如果您得到所有引用的列表,您可以列出所有不是另一个引用的父引用:
refs=`git show-ref -s`
git log --oneline --decorate $refs --not `echo "$refs" | sed 's/$/^/'`
这是通过过滤掉所有父提交来实现的
git show -s --oneline --decorate $(
git show-branch --independent -a
)
或
也许sed
'd适合您的口味。例如,要将所有引用名称拆分为每行一个,并去掉分组标点符号
git show -s --format=%d $(git show-branch --independent -a) \
| sed 's/,/\n/g; s/[ ()]//g'
已经有了一些有用的答案,但没有给出要求的内容:列表或分支(对我来说,这意味着分支名称,而不是提交ID)
获取tip分支名称列表
正在获取非tip分支名称的列表
整理东西时,您可能希望看到非尖端分支。这里有一个方法可以得到它们的列表。唯一的变化是&
变成|
{
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"
}
获取具有提交ID和消息的非提示分支名称列表
您可以根据自己的意愿进行装饰,例如通过更改:
echo $branchname
进入
结果如下所示:
** 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
** 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
** 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
然后,您可以在自己喜欢的工具中查看这些分支,看看删除它们是否有意义。由于Git保存了提交的父节点而不是子节点,因此在历史图中查找叶节点(您描述的“提示”)并不是那么简单。写一个脚本来了解你想知道的内容相对来说比较容易,但需要仔细检查你的回购协议中的所有提交。我想找到我可以删除的分支。例如,如果我将删除非叶节点中的分支,我将为自己清除无用的分支。任何非最终分支都可以恢复,因为它们的提交将在树中。我认为这不会起作用:不是所有的引用都在refs
目录中有一个文件,其中一些位于压缩的引用文件中。另外,出于某种原因,您的代码对我来说根本不起作用:它不返回任何内容。没有git show ref有git show ref,但您的行没有输出,您希望通过s/$/^/生成什么?例如:$echo$refs 722AACADEB64136D19DAB91C7516682C2A8C796 3b242412889ca1101914c2834858ebdc9efa4839 REF之间有空格。我尝试了s/\n/g,但它无助于定义叶节点:(这就是我一直在寻找的。剩下的另一个问题。如何验证所有这些引用都在另一个回购协议中,并且我可以安全地删除该回购协议。是否有选项?是的,我想查找C&E分支,而不是您示例中的A、B、D。例如,我有一个包含多个分支的存储库,我想清理分支引用。
{
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