GitPython 列出特定文件的所有提交

ben*_*enj 2 python git gitpython

我正在编写一个 Python 脚本,我需要知道特定文件的所有提交。在我的代码中,我使用GitPython来执行其他任务,但对于这个问题我找不到任何东西。

在命令行中我使用:

git log --pretty='%H' file-path
Run Code Online (Sandbox Code Playgroud)

小智 6

您可以查询您克隆的“repo”上的提交:

commits = repo.iter_commits('--all', max_count=100, since='10.days.ago', paths=path)
Run Code Online (Sandbox Code Playgroud)

...其中“-all”将返回所有分支和标签上的提交,路径是您的文件名。

要使用提交,您可以像这样:

for commit in commits:
  print("Committed by %s on %s with sha %s" % (commit.committer.name, time.strftime("%a, %d %b %Y %H:%M", time.localtime(commit.committed_date)), commit.hexsha))
Run Code Online (Sandbox Code Playgroud)