Code:
import os
from pathlib import Path
from git import Repo
# path to repo folder
path = Path(os.path.dirname(os.path.abspath(__file__)))
# repo variable
repo = Repo(path)
# Your last commit of the current branch
commit_feature = repo.head.commit.tree
# Your last commit of the dev branch
commit_origin_dev = repo.commit("origin/dev")
new_files = []
deleted_files = []
modified_files = []
# Comparing
diff_index = commit_origin_dev.diff(commit_feature)
# Collection all new files
for file in diff_index.iter_change_type('A'):
new_files.append(file)
# Collection all deleted files
for file in diff_index.iter_change_type('D'):
deleted_files.append(file)
# Collection all modified files
for file in diff_index.iter_change_type('M'):
modified_files.append(file)
Analyze:
Let’s imagine the situation:

First you need to get last commits from two branches:
# Your last commit of the current branch
commit_dev = repo.head.commit.tree
# Your last commit of the dev branch
commit_origin_dev = repo.commit("origin/dev")
Then get your changes:
diff_index = commit_origin_dev.diff(commit_feature)
And at last get sort changes by type:
# Collection all new files
for file in diff_index.iter_change_type('A'):
new_files.append(file)
# Collection all deleted files
for file in diff_index.iter_change_type('D'):
deleted_files.append(file)
# Collection all modified files
for file in diff_index.iter_change_type('M'):
modified_files.append(file)
Here you need to be careful, becase comparing commit_origin_dev with commit_feature and commit_feature with commit_origin_dev can give different results, when you are specify type change type.
More about:
- iter_change_type – https://gitpython.readthedocs.io/en/stable/reference.html?highlight=iter_change_type#git.diff.DiffIndex.iter_change_type
- diff – https://gitpython.readthedocs.io/en/stable/tutorial.html?highlight=iter_change_type#obtaining-diff-information and more api doc info https://gitpython.readthedocs.io/en/stable/reference.html?highlight=iter_change_type#module-git.diff