需要在 GitPython 中获取最新合并的提交 SHA id

Need to get commit SHA id of latest merge in GitPython

我在 git 中的默认分支是 'develop' 分支。

我想获取最新合并分支的 commit-id 到我的 'develop' 分支。 git python 可以吗?

在命令行我可以做到

git log | grep Merge

然后选择最新的一个。 有没有办法用 gitpython 做到这一点?

谢谢。

提交分支 HEAD 后 (headcommit = repo.head.commit),您可以:

  • 检查其 parent 的数量:len(headcommit.parents)
  • 如果一个,取它唯一的parent:headcommit.parents[0]

重复,直到找到包含多个提交的提交 parent:这将是您的合并提交。
那将模仿 git log --merges -n 1.

OP 推荐:

headcommit = repo.head.commit 
while True: 
  headcommit = headcommit.parents[0] 
  if len(headcommit.parents) is not 1: break 
print (headcommit