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)
To find out which files changed in a given commit, use the git log --raw command. It's the fastest and simplest way to get insight into which files a commit affects.
Viewing a list of the latest commits. If you want to see what's happened recently in your project, you can use git log . This command will output a list of the latest commits in chronological order, with the latest commit first.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With