Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List file that have changed since last commit with GitPython

Tags:

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)     
like image 280
Cmag Avatar asked Jan 22 '16 05:01

Cmag


People also ask

How to see list of files changed in a git commit?

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.

How to check last commit files in git?

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.


1 Answers

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) 
like image 134
alecxe Avatar answered Oct 19 '22 22:10

alecxe