List file that have changed since last commit with GitPython
Asked Answered
N

1

27

I need to have the Python script read in the files that have changed since the last Git commit. Using GitPython, how would I get the same output as running from cli:

$ git diff --name-only HEAD~1 HEAD

I can do something like the following, however, I only need the file names:

hcommit = repo.head.commit
for diff_added in hcommit.diff('HEAD~1').iter_change_type('A'):
    print(diff_added)    
Natator answered 22/1, 2016 at 5:11 Comment(0)
A
28

You need to pass the name_only keyword argument - it would automatically be used as --name-only command-line option when a git command would be issued.

The following is the equivalent of git diff --name-only HEAD~1..HEAD:

diff = repo.git.diff('HEAD~1..HEAD', name_only=True)
print(diff)
Anthropocentric answered 24/1, 2016 at 23:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.