如何在 master 上构建所有 git 提交的列表? [关闭]

Posted

技术标签:

【中文标题】如何在 master 上构建所有 git 提交的列表? [关闭]【英文标题】:How to build a list of all git commits on master? [closed] 【发布时间】:2021-05-23 02:28:56 【问题描述】:

给定一个 git 存储库,我想按日期列出分支 origin/master 上的所有提交及其 SHA 值。实现这一目标的最简单方法是什么?

我想要的结果是 Node.js 中代表 git 存储库的对象数组,其中包含提交数据,例如

    [
      
        date: "2020-02-02",
        sha: "03ffd2d7c3c1fdcc86f947537c6f3afa209948dd",
      ,
      
        date: "2019-03-13",
        sha: "3a7dbc7e6ab332ebbca9a45c75bd608ddaa1ef95",
      ,
      ...
    ]

或者只是一个逗号分隔的列表,例如

2020-02-02
03ffd2d7c3c1fdcc86f947537c6f3afa209948dd
2019-03-13
3a7dbc7e6ab332ebbca9a45c75bd608ddaa1ef95
...

【问题讨论】:

【参考方案1】:

因为您在这里提到了 node,所以我为您的问题提供了一个完全使用 node 环境的解决方案。

据我测试,这可能仅限于本地存储库,但我稍后会进行更多测试,并让您知道它是否也可以用于来自 github 的存储库。

为此,您需要 gitlog 模块。 gitlog npm page

您可以使用npm install gitlog 安装它(更多信息在上面提到的页面)。

// You need gitlog module to get and parse the git commits
const gitlog = require("gitlog").default ;

// You can give additional field names in fields array below to get that information too.
//You can replace `__dirname` with path to your local repository.
const options = 
    repo : __dirname,
    fields : ["hash", "authorDate"]


const commits = gitlog(options) ;

//logObject takes one parameter which is an array returned by gitlog() function
const logObject = commits => 
    let log = [] ;
    commits.forEach( value => 
        const hash = value.hash ;
        const date = value.authorDate ;
        log.push(hash, date) ;
    )
    return log ;


//This returns the results in an array 
logObject(commits) ;

//This returns the array in accending order
logObject(commits).sort((first, second) => 
    return Date.parse(first.date) - Date.parse(second.date) ;
) ;


//This returns the array in decending order
logObject(commits).sort((first, second) => 
    return Date.parse(second.date) - Date.parse(first.date) ;
) ;

【讨论】:

【参考方案2】:

最简单的方法是使用 git 开箱即用的功能。这是一个例子:

git log origin/master --date-order --format=%H%n%cs

【讨论】:

以上是关于如何在 master 上构建所有 git 提交的列表? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

如何删除推送的 git 合并提交

如何将一个git的master分支提交添加到另一个git中?

git没有提交的代码如何迁移到新建分支

在重新提交许多提交时如何防止许多 git 冲突?

如何查看 git 中分支之间的提交差异?

列出两个日期之间对 master 分支的 git 提交