我想获得Git存储库中所有分支的列表,其中“最新”分支位于顶部,“最新的”分支是最近提交的分支(因此,更可能是我想关注的分支)。

有没有一种方法可以使用Git(a)按最新提交对分支列表进行排序,或者(b)以某种机器可读格式将分支列表与每个分支的最后提交日期一起获取?

最坏的情况是,我总是可以运行gitbranch获取所有分支的列表,解析其输出,然后运行git-log-n 1 branchname--format=format:%ci获取每个分支的提交日期。但这将在Windows环境下运行,在那里启动一个新进程相对昂贵,因此如果有很多分支,每个分支启动一次Git可执行文件可能会很慢。有没有一种方法可以用一个命令完成所有这些?


当前回答

另一种变化:

git branch -r --sort=-committerdate --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always | column -ts'|'

值得注意的是,尽管它正在查看远程分支中的更改,但在运行命令之前还是值得与origin同步(您可以使用Git fetch),因为我发现如果您的本地Git文件夹有一段时间没有更新,它可能会返回过期信息。

此外,这是一个在Windows cmd和PowerShell中工作的版本:

git branch -r --sort=-committerdate --format="%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)" --color=always

其他回答

另一种变化:

git branch -r --sort=-committerdate --format='%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)' --color=always | column -ts'|'

值得注意的是,尽管它正在查看远程分支中的更改,但在运行命令之前还是值得与origin同步(您可以使用Git fetch),因为我发现如果您的本地Git文件夹有一段时间没有更新,它可能会返回过期信息。

此外,这是一个在Windows cmd和PowerShell中工作的版本:

git branch -r --sort=-committerdate --format="%(HEAD)%(color:yellow)%(refname:short)|%(color:bold green)%(committerdate:relative)|%(color:blue)%(subject)|%(color:magenta)%(authorname)%(color:reset)" --color=always

被接受的命令行回答令人震惊,但如果你想要更漂亮的东西,比如GUI,以及你的origin==“github”。

您可以单击存储库中的“分支”。或者直接点击URL:https://github.com/ORGANIZATION_NAME/REPO_NAME/branches

最简单的一个与最后提交日期一起打印:

git branch --all  --format='%(committerdate:short) %(refname:short)'|sort

以下是最佳代码,它结合了其他两个答案:

git for-each-ref --sort=-committerdate refs/heads/ --format='%(committerdate:short) %(authorname) %(refname:short)'

根据提交人日期,只获取排名前五的分支名称:

gitbranch--sort=-committerdate|head-5