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)    
Alan W. Smith
  • 24,647
  • 4
  • 70
  • 96
Cmag
  • 14,946
  • 25
  • 89
  • 140

1 Answers1

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)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195