import git repo_path = '/path/to/your/repository' file_path = 'path/to/your/file.txt' repo = git.Repo(repo_path) commit_list = list(repo.iter_commits(paths=file_path)) if len(commit_list) > 1: previous_commit = commit_list[1] previous_tree = repo.tree(previous_commit.tree) previous_filename = None for item in previous_tree.traverse(): if item.path == file_path: previous_filename = item.path break if previous_filename: print(f"The previous filename of {file_path} is {previous_filename}.") else: print(f"No previous filename found for {file_path}.") else: print(f"There is no previous commit for {file_path}.")