GitPython List all commits for a specific file

≡放荡痞女 提交于 2021-02-08 11:13:09

问题


I am writing a Python script and there I need to know all commits for a specific file. In my code I use GitPython for other tasks but for this problem I can't find something.

In cmd line I use:

git log --pretty='%H' file-path

回答1:


You can query the commits on the 'repo' that you cloned:

commits = repo.iter_commits('--all', max_count=100, since='10.days.ago', paths=path)

...whereby '-all' will return commits on all branches and tags, and path is your filename.

And to use the commits you go like:

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))



回答2:


What we are looking for in Git is:

git log --follow filename

not sure GitPython has it tho.



来源:https://stackoverflow.com/questions/39723521/gitpython-list-all-commits-for-a-specific-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!