【问题标题】:Azure Pipelines find commit hash of incoming mergeAzure Pipelines 找到传入合并的提交哈希
【发布时间】:2025-12-09 19:20:07
【问题描述】:

我对 git 比较陌生,遇到以下问题

我有一个管道,我正在尝试查找传入拉取请求/合并的提交哈希,存储它,然后在一个简单的 git log 命令中的其他地方使用它。

那么有没有 git 命令(或 powershell 脚本)可以找到 PR 的传入合并提交哈希?

如果答案很明显,请提前抱歉,我的 google fu 对此没有帮助。

【问题讨论】:

    标签: git powershell azure-devops azure-pipelines


    【解决方案1】:

    This command 应该可以工作。如果您只是传递 PR 编号,您将收到类型为 GitPullRequest 的 JSON 响应:

    _apis/git/repositories/{repositoryId}/pullrequests/{pullRequestId}
    

    {pullRequestId} 可能是 1234,例如。

    JSON 响应中包含不同的提交 ID,包括 lastMergeCommitlastMergeSourceCommitlastMergeTargetCommit。查看这些 ID 以确认您想要哪个。您还可以访问 PR 中包含的 commits 数组。

    【讨论】:

      【解决方案2】:

      如果您想查看上次合并中合并的每个提交,您可以尝试:

      git log $(git merge-base --octopus $(git log -1 --merges --pretty=format:%P)).. --boundary
      

      查看此案例了解更多详情:Show commits involved in a prior git merge

      【讨论】: