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

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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