git - 以特殊格式收集存储库中所有分支的列表 [Git]

标签 git version-control git-branch git-log

有没有办法收集存储库中的分支列表以及最后提交日期?

换句话说,我想将这样的内容打印到标准输出:

branch_name_1 date1
branch_name_2 date2
branch_name_3 date3

这可能吗?

编辑:我尝试使用以下步骤:

  1. git log --pretty=format:"%ad:%an:%d:%B"--date=short --branches

  2. git branch -a

  3. git ls-remote –heads

它们中的每一个都为我提供了我当前所在的存储库的分支。 但是现在我想检查是否可以从每个目录(到特定的 repo )运行命令。也以特殊格式打印。仍在尝试了解如何获取上次提交的日期。

Another-Edit:我考虑了一下,最好使用 git ls-remote –heads 因为我想检查一个非本地 repo。我如何检查 git ls-remote –heads 输出中每个分支的日期?

最佳答案

如果你的 Git 版本 >= 2.13,你可以使用相同的 git branch命令执行此操作。

它有一个 --format参数,您可以根据需要使用它来格式化输出。只需像这样运行它即可获得所需的输出。

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

您可以添加一些颜色以使其清晰。

git branch --format='%(color:bold green) %(refname:short) %(color:white) %(committerdate)' --sort=-committerdate

输出:

Development  Fri Jun 29 10:32:43 2018 +0530  
feat-2180  Fri Jun 8 18:01:36 2018 +0530  
master  Wed May 16 17:19:21 2018 +0530

这是它支持的字段名称列表 - https://github.com/git/git/blob/v2.17.0/ref-filter.c#L328

============================================= ======================

编辑:

正如您在评论中提到的;我认为您无法使用 ls-remote 获取提交详细信息单独指挥。刚刚在这里查看了命令的源代码,它似乎没有返回任何其他值,除了 refs 和提交哈希值。所以我认为不可能使用它(如果我错了请纠正我)。

https://github.com/git/git/blob/maint/builtin/ls-remote.c

如果您可以进行临时克隆,我建议您创建一个 shell 脚本或某种脚本来进行克隆并获取摘要。

这是一个例子:

#!/usr/bin/env bash

REMOTE_URL=$1

git clone -q --depth 1 --bare --no-single-branch $REMOTE_URL /tmp/temp-git-clone
cd /tmp/temp-git-clone
git branch --format='%(color:bold green) %(refname:short) %(color:white) %(committerdate)' --sort=-committerdate
rm -rf /tmp/temp-git-clone

我在 clone 中使用了以下参数命令使它更快一点。

--depth 1 = will get only the last commit

--bare = create a bare repository, which means there will be no working directory and it won't copy the files.

--no-single-branch = need this to tell the clone command to get all branches

如果您运行脚本:./summary.sh <your repo url>

它会按照您的预期显示摘要!

希望对您有所帮助:)

关于git - 以特殊格式收集存储库中所有分支的列表 [Git],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51362007/

相关文章:

Git 和 SSH,使用哪个 key ?

php - 在没有 Git 的情况下读取 Git 存储库

version-control - 是否有一种简单的方法可以还原整个 P4 更改列表?

git - 在错误的分支中提交 2 次后该怎么办

git - 从本地 git 存储库中删除所有时间戳

git - 阻止新分支获取 Git 中的旧提交历史记录

git - 是否可以 merge 从同一 CVS 源导入的不相交的 Git 存储库?

android - 同步Gradle失败

git - 如何将 readme.md 重命名为 README.md?

cocoa - Xcode 适合哪种单片机系统?