Gitpython List All Commits for a Specific File

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

2 Answers

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

What we are looking for in Git is:

git log --follow filename

not sure GitPython has it tho.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Marcus Vance

Marcus Vance

Cybersecurity & Digital Privacy Researcher

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.

Share this article
Twitter Facebook Pinterest