首页 > 解决方案 > How to get list of modified files of a Git repo in python?

问题描述

I am trying to write a function using python to determine modified files in a git repository. I saw there are couple of packages:

How can I get a list of pathlib.Path instances of modified files?

Python version in use: 3.8

Edit:

In order to check modified files in GitPython I have tried the following:

diff = repo.git.diff('HEAD~1..HEAD', name_only=True)

but this gives me the files that are different, comparing the latest commit with the one before, instead of comparing the latest commit with unstaged changes.

标签: pythongit

解决方案


To get a definitive list of what's changed (but not yet staged):

# Gives a list of the differing objects
diff_list = repo.head.commit.diff()

for diff in diff_list:
    print(diff.change_type) # Gives the change type. eg. 'A': added, 'M': modified etc.

    # Returns true if it is a new file
    print(diff.new_file) 

    # Print the old file path
    print(diff.a_path)

    # Print the new file path. If the filename (or path) was changed it will differ
    print(diff.b_path) 

# Too many options to show. This gives a comprehensive description of what is available
help(diff_list[0]) 

I found the diff object to be very useful and should give any info you require.

For staged items, use repo.index

The other option is repo.git.diff(...) which I found less useful as it gives long text strings for output rather than objects that can be easily parsed.


推荐阅读